我想将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 ,发生内部服务器错误,服务器返回消息
{
"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 ..."
} }
答案 0 :(得分:0)
我找到了! ResponseModel 类dos没有 DataContract和DataMember 属性。