c# - ASP.NET WebAPI 序列化问题

标签 c# asp.net asp.net-web-api serialization

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

我已经应用了这些属性,但是,我仍然看到错误。

出现错误的代码是:

[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 定义为:

[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)。

错误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.

错误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 ...

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

还有其他人看到过这个吗?或者有人可以帮助阐明这个问题吗?

最佳答案

看起来您的问题可能是您正在从响应中创建响应...您创建了一个响应,然后如果不为空,则尝试使用原始响应创建另一个响应。所以,我想它正在尝试序列化原始响应,而不是你的对象。

关于c# - ASP.NET WebAPI 序列化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50723883/

相关文章:

c# - TcpClient - 现有连接被远程主机强行关闭

c# - 加载到 XDocument 时如何解析实体?

asp.net - 如何使用自定义目标 "PublishToFileSystem"从发布中排除APP_DATA目录

c# - 如何在 lambda 中使用 async/await

c# - 如何在.net core WebApi 上获取请求字符串

c# - 在 C# 中播放声音 - 使用所有方法中的哪一种?

c# - 如何使用 StructureMap (4.0) 将依赖项注入(inject)自定义属性? MVC5项目

c# - 提交时下拉值重置为初始值

c# - 从自定义 AdditionalMetadataAttribute (asp.net mvc 5) 访问模型类实例

asp.net - 您可以在 .NET WebApi Route URI 中使用正则表达式进行 API 版本控制吗