Friday 16 March 2012

HttpBinding and TcpBinding

Problem
I would like to use HttpBinding and TcpBinding in the same service in WCS.


Impact
Use the same service with different Bindings a t the same time.

Solution
You can use the following code or change the configuration file as below:

1.      Code:
Uri httpAddress = new Uri("http://localhost:8000/OrderService/");
Uri tcpAddress = new Uri("net.tcp://localhost:8001/OrderService/");
Uri[] baseAddresses = { httpAddress, tcpAddress };

ServiceHost host = new
            ServiceHost(typeof(MyNamespace.OrderService), baseAddresses);

BasicHttpBinding basicBinding = new BasicHttpBinding();
WSHttpBinding wsBinding = new WSHttpBinding();
NetTcpBinding netBinding = new NetTcpBinding();
host.AddServiceEndpoint(typeof(MyNamespace.IOrderService),
                         basicBinding, "");
host.AddServiceEndpoint(typeof(MyNamespace.IOrderService),
                         wsBinding, "secure");
host.AddServiceEndpoint(typeof(MyNamespace.IOrderService),
                         netBinding, "");

2.      Configuration File:

<configuration>
  <system.serviceModel>
    <services>
      <service name="OrderService">
        <endpoint address="http://localhost:8000/OrderService/"
                  contract="MyNamespace.IOrderService"
                  binding="BasicHttpBinding">
        </endpoint>
        <endpoint address="http://localhost:8000/OrderService/secure"
                  contract="MyNamespace.IOrderService"
                  binding="wsHttpBinding">
        </endpoint>
        <endpoint address="net.tcp://localhost:8001/OrderService/"
                  contract="MyNamespace.IOrderService"
                  binding="NetTcpBinding">
        </endpoint>
      </service>
    </services>
  </system.serviceModel>
</configuration>

Conclusion
You can use differente types of binding to use the same service, the addresses have to be different for the different binding, al least the contracts are different.

No comments:

Post a Comment