2015-04-03 128 views
0

我想將WebApi響應序列化爲ProtoBuf格式。ProtoBuf在WebApi中序列化異常HttpResponseMessage

[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.FormattingMediaTypeFormatter,內部服務器錯誤發生,服務器返回以下消息

{ 
    "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

我找到了! ResponseModel類dos沒有DataContract和DataMember屬性。