1

我正在使用 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);

想法?

4

1 回答 1

2

您是否尝试过其他的东西IEnumerable<Product>特别是,可能是具有一组s的顶级对象Product?例如:

[ProtoContract]
public class NeedsBetterName {
    [ProtoMember(1)]
    public List<Product> Products {get;set;}
}

可能会更快乐。实际上,我在本月早些时候(在 r629 中)添加了对“裸可枚举”的更好支持——但是,我还没有在任何地方部署这个构建。

于 2013-04-26T20:03:30.087 回答