ASP.NET WebAPI序列化问题 [英] ASP.NET WebAPI Serialization Issues

查看:86
本文介绍了ASP.NET WebAPI序列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我无法在WebAPI项目中跟踪其来源.该API一直在起作用,但是,在部署时我发现我收到一个与对象序列化有关的错误,这暗示我需要在类上的 DataContract 属性和 DataMember 属性在每个可序列化的属性上.

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.

显示错误的代码是:

[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");
}

其中PortalUser定义为:

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; }
}

如您所见,我已经尝试添加错误中建议的属性(下面的错误1).我还尝试了删除 XmlMediaFormatter ,然后开始抛出关于无法访问流上的 ReadTimeout 的错误(下面的错误2).

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).

错误1:

类型'System.Net.Http.ObjectContent`1 [PolicyService.Common.Models.PortalUser]'无法序列化.考虑使用DataContractAttribute属性,并将其标记为您的所有成员要使用DataMemberAttribute属性序列化.如果类型是集合,请考虑将其标记为CollectionDataContractAttribute.请参阅Microsoft .NET Framework其他受支持类型的文档.

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.

错误2:

"Message":发生错误.","ExceptionMessage":获取错误从"ReadTimeout"开始的值'Microsoft.Owin.Host.SystemWeb.CallStreams.InputStream'.," ExceptionType:" Newtonsoft.Json.JsonSerializationException," StackTrace:"在Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object目标)\ r \ nNewtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter作者,对象值,JsonContainer合同,JsonProperty成员,JsonProperty属性,JsonContract&memberContract,Object&memberValue)\ r \ n位于...

"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 ...

我见过类似错误消息的实例,但是,听起来大多数错误消息都是通过添加 DataContract 属性来解决的,在这里没有帮助.

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?

推荐答案

看起来您的问题可能是您正在根据响应创建响应...您创建了响应,如果不是null,则尝试创建另一个回应与原始回应.因此,我想它正在尝试序列化原始响应,而不是您的对象.

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.

这篇关于ASP.NET WebAPI序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆