Introduction
Recently, I was asked whether a Windows Communication Foundation (WCF) service can have both net.tcp and http binding. I did a proof of concept (POC) work and want to share with you here.
How to
Actually, there is a good business/technical reasons behind creating services with multiple endpoints. For example, you may have a service that is consumed both your application and external clients. Now, you may use net.tcp binding for internal usage (performance reasons), and http binding for external usages (interoperability reasons).
To accomplish this, there are 3 major things you need to do:
- Define specific addresses for each endpoint within baseAddresses section.
- Name your service endpoints.
- Adjust client side configuration section accordingly after referenced.
If you ignore naming service endpoints, you will get the following error:
‘An endpoint configuration section for contract ‘…’ could not be loaded because more than one endpoint configuration for that contract was found. Please indicate the preferred endpoint configuration section by name.’
For your convenience, service configuration displayed below:
... <services> … <service behaviorConfiguration="serviceBehavior" name="Demo1.ServiceHost.Self.Service1"> <host> <baseAddresses><add baseAddress="http://localhost:8777/DesignTime/Demo1/Service1" /><add baseAddress ="net.tcp://localhost:8767/DesignTime/Demo1/Service1"/></baseAddresses></host><endpoint address="withHttp" binding="basicHttpBinding" name="bHttpService1"contract="Demo1.ServiceHost.Self.IService1" /><endpoint address="withTcp" binding="netTcpBinding" name="tcpService1"contract="Demo1.ServiceHost.Self.IService1" /><endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange" /><endpoint address="mex" binding="mexTcpBinding" name="mex" contract="IMetadataExchange" /></service></services> <behaviors> <serviceBehaviors><behavior name="serviceBehavior"><serviceThrottling maxConcurrentCalls="200" maxConcurrentInstances="2000" maxConcurrentSessions="2000"/><dataContractSerializer maxItemsInObjectGraph="2147483647"/><serviceMetadata httpGetEnabled="true"/><serviceDebug includeExceptionDetailInFaults="true"/></behavior></serviceBehaviors></behaviors> |