WebApi HttpResponseMessage 中的 ProtoBuf 序列化异常
ProtoBuf serialize exception in WebApi HttpResponseMessage
我想将 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
};
}
我在生成 HttpResponseMessage 对象时使用 WebApiContrib.Formatting 作为 MediaTypeFormatter,发生内部服务器错误并且服务器 return下面留言
{
"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 ..."
} }
我找到了!
ResponseModel class 没有 DataContract 和 DataMember 属性。
我想将 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
};
}
我在生成 HttpResponseMessage 对象时使用 WebApiContrib.Formatting 作为 MediaTypeFormatter,发生内部服务器错误并且服务器 return下面留言
{
"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 ..."
} }
我找到了! ResponseModel class 没有 DataContract 和 DataMember 属性。