【问题标题】:ProtoBuf serialize exception in WebApi HttpResponseMessageWebApi HttpResponseMessage 中的 ProtoBuf 序列化异常
【发布时间】:2015-04-04 04:08:05
【问题描述】:

我想将 WebApi 响应序列化为 ProtoBuf 格式。

[HttpGet]
public HttpResponseMessage Foo()
{
    var boo = new BooMolde();
    return new HttpResponseMessage
    {
        Content = new ObjectContent(typeof(ResponseModel), new ResponseModel
        {
            Status = 1, 
            Data = boo
        }, new ProtoBufFormatter()),
        StatusCode = HttpStatusCode.Ok
    };
}

我使用 WebApiContrib.Formatting 作为 MediaTypeFormatter 生成 HttpResponseMessage 对象,发生内部服务器错误,服务器返回以下消息

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "The 'ObjectContent' type failed to serialize the response body for content type 'application/x-protobuf'.",
    "ExceptionType": "System.InvalidOperationException",
    "StackTrace": null,
    "InnerException": {
        "Message": "An error has occurred.",
        "ExceptionMessage": "Type is not expected, and no contract can be inferred: ###",
        "ExceptionType": "System.InvalidOperationException",
        "StackTrace": "   at ProtoBuf.Meta.TypeModel.ThrowUnexpectedType(Type type) in ..."
    } }

【问题讨论】:

    标签: c# asp.net-web-api protocol-buffers protobuf-net


    【解决方案1】:

    我找到了! ResponseModel 类没有 DataContract 和 DataMember 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 2021-02-17
      相关资源
      最近更新 更多