【发布时间】: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