Adding protobuf-net to my WCF Rest service

Is there any canonical direct way to enable protobuf-net serialization on .NET 4 WCF? I tried to simplify the code to such an extent that it could be easily built:

Here is my service code:

[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; } } 

I run this service route in Application_OnStart (Global.asax) as follows:

 RouteTable.Routes.Add(new ServiceRoute("mobile", new MyServiceHostFactory(), typeof(MobileServiceV2))); 

I use MyServiceHostFactory to manage services using MEF, but that doesn't matter.

My default service configuration is the only additional material in 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> 

Good, the service is up and running. There is mobile / help, I can issue GET on mobile / some data and get a response in both XML and JSON

application / xml returns XML, application / json returns JSON

What do I need to do so that clients can install the / x -protobufs application and get their response encoded with protobuf-net?

I read all day and get confused more and more ...

Here is what I have found so far, and nothing seems to give a direct solution:

The second link is what I need, but it does not work for me. I don’t know why, but I can’t understand what namespace MediaTypeProcessor lives in and simply cannot make it work in .NET4?

The various scattered information about configuring protobuf-net via web.config gives me different errors, and I'm just not sure if I need to go that way. I would rather have only a solution code.

EDIT:

From what I explored - my bad one, MediaFormatter doesn't seem to be in the current version of WCF. I wonder if it would be best to create a separate URL for all protobuf clients? That way I can get Stream and return Stream. The handler performs all the serialization of the logic manually. A little more work, but I will better control the actual data.

+4
source share
1 answer

The first thing to do is add Microsoft.AspNet.WebApi ("Basic ASP.NET Core Web Application Libraries" (RCA) "and Microsoft.AspNet.WebApi.Client (" Web API Client Libraries (RC) ") via NuGet.

This looks like the most promising walkthrough: http://byterot.blogspot.co.uk/2012/04/aspnet-web-api-series-part-5.html

MediaTypeFormatter is located in System.Net.Http.Formatting

I don’t have time right now to try to make it work, but you can PRESUMABLY add a formatter via:

 GlobalConfiguration.Configuration.Formatters.Add( new ProtobufMediaTypeFormatter(RuntimeTypeModel.Default)); 

In the example, a TOTALLY UNVESTED implementation of something like:

 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; } } 

I don't know anything about the web API, so please let me know if you manage to get it working for you. I will rather happily add a supported shell as a downloadable binary, but I cannot do this until it works; p

+3
source

All Articles