ASP.NET WebAPI Serialization Issues

2019-07-20 06:34发布

I have an issue that I can't trace the origin of within my WebAPI project. The API has been working, however, when deploying I found I was receiving an error relating to serialization of an object that implied I needed a DataContract attribute on the class and DataMember attributes on each serializable property.

I've applied these attributes, however, I still see the error.

The code that presents the error is:

[ResponseType(typeof(PortalUser))]
public HttpResponseMessage Get([FromUri]int userId)
{
    var user = Request.CreateResponse(repository.GetById(userId));
    if (user != null)
        return Request.CreateResponse(user);
    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not found");
}

Where PortalUser is defined as:

[Serializable]
[DataContract]
public class PortalUser : IUser<string>
{
    public PortalUser() { }
    [DataMember]
    public string Id { get; set; }
    [DataMember]
    public string EmailAddress { get; set; }
    [DataMember]
    public string MobileTelephone { get; set; }
    [DataMember]
    public string Firstname { get; set; }
    [DataMember]
    public string Surname { get; set; }
    [DataMember]
    public string Company { get; set; }
    [DataMember]
    public string HashedPassword { get; set; }
    [DataMember]
    public string PasswordSalt { get; set; }
    [DataMember]
    public byte[] AuthenticatorQrCodeImage { get; set; }
    [DataMember]
    public string AuthenticatorFallbackCode { get; set; }
    [DataMember]
    public int FailedLoginCount { get; set; }
    [DataMember]
    public DateTime LastFailedLoginAttempt { get; set; }
    [DataMember]
    public string ManagerId { get; set; }
    [DataMember]
    public string UserName { get { return EmailAddress; } set { EmailAddress = value; } }
    [DataMember]
    public string TwoFactorAuthenticationSecretKey { get; set; }
}

As you can see, I've already tried adding the attributes suggested in the error (Error 1 below). I have also tried removing the XmlMediaFormatter, which then started throwing errors about not being able to access the ReadTimeout on a stream (Error 2 below).

Error 1:

Type 'System.Net.Http.ObjectContent`1[PolicyService.Common.Models.PortalUser]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

Error 2:

"Message":"An error has occurred.","ExceptionMessage":"Error getting value from 'ReadTimeout' on 'Microsoft.Owin.Host.SystemWeb.CallStreams.InputStream'.","ExceptionType":"Newtonsoft.Json.JsonSerializationException","StackTrace":" at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)\r\n at ...

I've seen instances of similar error messages, however, it sounds like most of these were resolved by adding the DataContract attributes, which hasn't helped here.

Has anyone else seen this, or can anyone help shed any light on the issue?

1条回答
成全新的幸福
2楼-- · 2019-07-20 07:29

It looks like your issue might be that you are creating a response out of a response...You create a response, then if not null, try to create another response with the original response. So, I imagine it is trying to serialize the original response, not your object.

查看更多
登录 后发表回答