2013-04-26 39 views
1

我正在使用WebApiContrib.Formatting.ProtoBuf NuGet包(http://nuget.org/packages/WebApiContrib.Formatting.ProtoBuf/0.9.5.0)在我的Web API项目中添加对协议缓冲区的支持。在客户端使用WebApiContrib ProtoBufFormatter

服务器端似乎工作得很好,但我一直无法获得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); 

想法?

回答

2

你有东西试过其他IEnumerable<Product>?特别是,也许顶级对象一套Product s?例如:

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

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

+0

这确实工作,虽然在我的情况下,我试图公开一个OData端点。至少使用Web API,我不确定如何告诉Web API将结果封装在像这样的容器类中。任何想法,当你将这个功能公开可用? – RMD 2013-04-29 13:47:15

+0

@RMD我在本周远程工作,无法访问我通常的虚拟机进行清理构建/回归测试;我可以在下周运行一个合适的版本 – 2013-04-29 14:33:17

+0

太棒了!谢谢马克。我们只是一个新项目的开始,并且很乐意提供protobuf作为选项。 – RMD 2013-04-29 14:40:24