将protobuf-net添加到我的WCF Rest服务

kat*_*tit 5 .net rest wcf protocol-buffers protobuf-net

是否有任何规范的直接方式在.NET 4 WCF上启用protobuf-net序列化?我试图将代码简化到可以轻松构建的程度:

这是我的服务代码:

[ServiceContract]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MobileServiceV2
{
    [WebGet(UriTemplate = "/some-data")]
    [Description("returns test data")]
    public MyResponse GetSomeData()
    {
        return new MyResponse { SomeData = "Test string here" };
    }
}

[DataContract]
public class MyResponse
{
    [DataMember(Order = 1)] 
    public string SomeData { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我正在Application_OnStart(Global.asax)中激活此服务路由,如下所示:

RouteTable.Routes.Add(new ServiceRoute("mobile", new MyServiceHostFactory(), typeof(MobileServiceV2)));
Run Code Online (Sandbox Code Playgroud)

我用MyServiceHostFactoryMEF来管理服务,但那是无关紧要的.

我的服务配置都是默认的,Web.Config中唯一的附加功能就在这里:

<system.serviceModel>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint helpEnabled="true" maxReceivedMessageSize="5242880" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </standardEndpoint>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

好的,服务还活着.有移动/帮助,我可以在移动/某些数据上发出GET并在XML和JSON中获得响应

application/xml返回XML,application/json返回JSON

我需要做什么才能让客户端设置application/x-protobufs并使用protobuf-net编码他们的响应?

我整天都在读书,越来越困惑......

这是我到目前为止所发现的,似乎没有什么可以直接解决:

  1. http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/

  2. http://weblogs.thinktecture.com/cweyer/2010/12/using-jsonnet-as-a-default-serializer-in-wcf-httpwebrest-vnext.html

第二个链接是我需要的东西,但它对我不起作用.不知道为什么,但我无法弄清楚哪个命名空间MediaTypeProcessor存在,只是不能使它在.NET4中工作?

关于通过web.config配置protobuf-net的各种分散信息给了我不同的错误,我只是不确定我是否需要这样做.我宁愿只使用代码解决方案.

编辑:

从我的研究 - 我的坏,MediaFormatter似乎不是在当前版本的WCF.我想知道为所有protobuf客户创建单独的URL是否最好?这样我就可以收到Stream并返回Stream.处理程序所有手动序列化逻辑.更多的工作,但我会更好地控制实际数据.

Mar*_*ell 3

首先要做的是通过 NuGet 添加Microsoft.AspNet.WebApi("Microsoft ASP.NET Web API Core Libraries (RC)") 和Microsoft.AspNet.WebApi.Client("Microsoft ASP.NET Web API Client Libraries (RC)")。

这看起来像是最有希望的演练:http://byterot.blogspot.co.uk/2012/04/aspnet-web-api-series-part-5.html

MediaTypeFormatter是在System.Net.Http.Formatting

我现在没有时间尝试让它工作,但您可以通过以下方式添加格式化程序:

GlobalConfiguration.Configuration.Formatters.Add(
    new ProtobufMediaTypeFormatter(RuntimeTypeModel.Default));
Run Code Online (Sandbox Code Playgroud)

通过一个完全未经测试的实现示例,例如:

public class ProtobufMediaTypeFormatter : MediaTypeFormatter
{
    private readonly TypeModel model;
    public override bool CanReadType(Type type)
    {
        return model.IsDefined(type);
    }
    public override bool CanWriteType(Type type)
    {
        return model.IsDefined(type);
    }
    public ProtobufMediaTypeFormatter() : this(null) {}
    public ProtobufMediaTypeFormatter(TypeModel model) : base()
    {
        this.model = model ?? RuntimeTypeModel.Default;
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/protobuf"));
    }
    public override System.Threading.Tasks.Task<object> ReadFromStreamAsync(Type type, System.IO.Stream stream, HttpContentHeaders contentHeaders, IFormatterLogger formatterLogger)
    {
        // write as sync for now
        var taskSource = new TaskCompletionSource<object>();
        try
        {
            taskSource.SetResult(model.Deserialize(stream, null, type));
        } catch (Exception ex)
        {
            taskSource.SetException(ex);
        }
        return taskSource.Task;
    }
    public override System.Threading.Tasks.Task WriteToStreamAsync(Type type, object value, System.IO.Stream stream, HttpContentHeaders contentHeaders, System.Net.TransportContext transportContext)
    {
        // write as sync for now
        var taskSource = new TaskCompletionSource<object>();
        try
        {
            model.Serialize(stream, value);
            taskSource.SetResult(null);
        }
        catch (Exception ex)
        {
            taskSource.SetException(ex);
        }
        return taskSource.Task;
    }
}
Run Code Online (Sandbox Code Playgroud)

我对 Web API 几乎一无所知,所以如果您设法让它为您工作,请告诉我。我很乐意将受支持的包装器添加为可下载的二进制文件,但在它工作之前我不能这样做;p