Thursday, May 25, 2017

Binding in WCF-21

What is Binding in WCF service?

Binding defines how the client will communicate with WCF service. Binding tell about three things in communication. There are different binding available in WCF like basiHttpBinding, wsHttpBinding, TCP,....

1. Transport Protocol (ex - HTTP, TCP, NamedPipe, MSMQ)
2. Message Encoding ( ex. text/Xml, Binary)
3. Protocol (ex. reliable messaging, transport support)

In WCF you can create your custom binding also.

For speed you can choose netTCP binding, if you have same platform on both end.

For communication client and WCF service should same configuration entry.

Configure WCF service endpoint dynamically in code

Endpoint and behavior setting can be write in code instead of config file.

As depicted below. It is written in host.

 using (ServiceHost host = new ServiceHost(typeof(CalculatorService.CalculatorService)))
                ServiceMetadataBehavior serviceMetadataBehavior = new ServiceMetadataBehavior()

                host.AddServiceEndpoint(typeof(CalculatorService.ICalculatorService), new BasicHttpBinding(), "CalculatorService");

                WriteLine("Host Started @ " + DateTime.Now.ToString());

And app.config file would defined as below

      <service name="CalculatorService.CalculatorService">
            <add baseAddress="http://localhost:9001"/>


No comments: