WCF Use Protobuf

Source: Internet
Author: User

PROTOBUF has ultra-high transmission efficiency, can replace the XML and binary encoding of WCF, can try

Create a new WCF service library in VS2013, with the name using the default WcfServiceLibrary1

Create a new console program in the current solution, named client

Using NuGet to install proto-net, why not use the latest 2.1.0 version? Because you want to eject the error protobuf-net "already has a dependency defined for" Netstandard.library ".

Install-package protobuf-net-version 2.0.0.668-projectname WcfServiceLibrary1

Install-package protobuf-net-version 2.0.0.668-projectname Client

Copy the Wcfservicelibrary1\packages\protobuf-net.2.0.0.668\lib\net40\protobuf-net.dll to

\wcfservicelibrary1\wcfservicelibrary1

Wcfservicelibrary1\client

Registration Behavior Extension : Copy the following code to <system.serviceModel> below

    <extensions>      <behaviorExtensions>        <add name= "Protobuf" type= " ProtoBuf.ServiceModel.ProtoBehaviorExtension, Protobuf-net, version=2.0.0.668, Culture=neutral, publickeytoken= 257b51d87d2e4d67 "/>      </behaviorExtensions>    </extensions>

Apply behavior Extensions to endpoint behavior : Copy under <behaviors>

      <endpointBehaviors>        <behavior name= "Protoendpointbehavior" >          <protobuf/>        </ Behavior>      </endpointBehaviors>

And let the service use this endpoint behavior and add it under <endpoint>.

behaviorconfiguration= "Protoendpointbehavior"

The final look of app. Config

<?xml version= "1.0" encoding= "Utf-8"?><configuration> <system.web> <compilation debug= "true"/&  Gt </system.web> <system.serviceModel> <extensions> <behaviorExtensions> <add Name = "Protobuf" type= "ProtoBuf.ServiceModel.ProtoBehaviorExtension, Protobuf-net, version=2.0.0.668, Culture=neutral, Publickeytoken=257b51d87d2e4d67 "/> </behaviorExtensions> </extensions> <services> &l T;service name= "Wcfservicelibrary1.service1" > <endpoint address= "" behaviorconfiguration= "ProtoEndpointBehav            IOR "binding=" BasicHttpBinding "contract=" Wcfservicelibrary1.iservice1 "> <identity> <dns value= "localhost"/> </identity> </endpoint> <endpoint address= "Mex" bind ing= "mexHttpBinding" contract= "IMetadataExchange"/> 

After the configuration file has been written, you also need to modify the IServer.cs, here is just introduction, only Compositetype as an example, need to add protocontract, protomember two kinds of features

    [DataContract]    [Protocontract]    public class Compositetype    {        bool boolvalue = true;        String stringvalue = "Hello";        [DataMember]        [Protomember (1)]        public bool Boolvalue        {            get {return boolvalue;}            set {boolvalue = value;}        }        [DataMember]        [Protomember (2)]        public string StringValue        {            get {return stringvalue;}            set {stringvalue = value;}        }    }
    Public interface IService1 also add the [ServiceContract] attribute

The client references the WCF service because the WCF service is in this project, so select the service in the solution

The

   client also wants to add the extension and endpoint behavior just now, and eventually the app. config is like this

<?xml version= "1.0" encoding= "Utf-8"?><configuration> <system.serviceModel> <extensions> <behaviorExtensions> <add name= "protobuf" type= "ProtoBuf.ServiceModel.ProtoBehaviorExtension, pro      Tobuf-net, version=2.0.0.668, culture=neutral, publickeytoken=257b51d87d2e4d67 "/> </behaviorExtensions> </extensions> <bindings> <basicHttpBinding> <binding name= "Basic            Httpbinding_iservice1 "/> </basicHttpBinding> </bindings> <client> <endpoint address= "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/" Behaviorc onfiguration= "Protoendpointbehavior" binding= "BasicHttpBinding" bindingconfiguration= "BasicHttpBinding_ISer Vice1 "contract=" Servicereference1.iservice1 "name=" Basichttpbinding_iservice1 "/> </client&gt      ; <behaviors>        <endpointBehaviors> <behavior name= "Protoendpointbehavior" > <protobuf/> </behavior> </endpointBehaviors> </behaviors> </system.servicemodel></conf Iguration>

OK, you can invoke the service on the client.

static void Main (string[] args) {    var proxy =new servicereference1.service1client ();    var result=proxy. Getdatausingdatacontract (New Servicereference1.compositetype () {stringvalue= "Test}");    Console.WriteLine (Result. StringValue);}

Can get the result is null

Look at other people's blog, the original protobuf is not a WCF legitimate, through the service reference, and will not like DataMember this natively supported attribute, the Protomember to the client's auto-generated code, so also need to add manually, egg pain AH

Found property in open Reference.cs public bool boolvalue add Protomember (1)]

Find property public string stringvalue Add [Protomember (2)]
It's finally coming to a conclusion.

PostScript, PROTOBUF is not prepared for WCF, but should be used in conjunction with GRPC, in the GRPC sample document can be seen as a very simple. proto file compiled into a complex CS file, and then respectively by the server and the client reference, the final implementation of remote invocation, but the sample environment is VS2015

Code

WCF Use Protobuf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.