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.