我正在使用 WebApiContrib.Formatting.ProtoBuf NuGet 包 ( http://nuget.org/packages/WebApiContrib.Formatting.ProtoBuf/0.9.5.0 ) 在我的 Web API 项目中添加对协议缓冲区的支持。
服务器端似乎工作得很好,但我无法让 Web API 客户端库反序列化服务器响应。
System.InvalidOperationException: Type is not expected, and no contract can be inferred: System.Collections.Generic.IEnumerable`1[[WebAPITest1.Protocol.Messages.Product, WebAPITest1.Protocol.Messages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
这是我的客户代码:
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(@"http://localhost:60500/");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-protobuf"));
var response = httpClient.GetAsync("/api/products?$orderby=Name").Result;
Assert.IsTrue(response.IsSuccessStatusCode);
var products = response.Content.ReadAsAsync<IEnumerable<Product>>(new[]{new ProtoBufFormatter()}).Result;
Assert.IsTrue(products.Count() > 0);
想法?