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()
                {
                    HttpGetEnabled=true
                };


                host.Description.Behaviors.Add(serviceMetadataBehavior);
                host.AddServiceEndpoint(typeof(CalculatorService.ICalculatorService), new BasicHttpBinding(), "CalculatorService");

                host.Open();
                WriteLine("Host Started @ " + DateTime.Now.ToString());
                ReadKey();
                host.Close();
            }

And app.config file would defined as below

  <system.serviceModel>
    <services>
      <service name="CalculatorService.CalculatorService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9001"/>
          </baseAddresses>
        </host>
      
      </service>
    </services>
  

  </system.serviceModel>

No comments:

Followers

Link