Monday, August 3, 2009

Tip: Seting up the WCF wsDualHttpBinding or Duplex Service the correct way.

If you want to use the WCF wsDualHttpBinding to establish a consistent communication between a client and a server (i.e. server to send data to the client consistently), WCF has to create 2 channels that connect from the client to the server and from the server to the client respectively. In order to allow WCF to setup the links correctly, you have to ensure that the client and the server configurations are correct.
Programming a dual binding WCF program will require 2 ports, one for client to communicate with the server and one for the server to communicate back to the client. Basically when debugging a WCF service in your local machine, an example of the server configuration file looks like


The configuration file for the client looks like

<system.serviceModel>

<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IStockService1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" clientBaseAddress="http://localhost:8989/client/"
transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" />
<security mode="Message">
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/DuplexGood/StockService.svc"
binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_IStockService1"
contract="ServiceReference1.IStockService" name="WSDualHttpBinding_IStockService1">
<identity>
<userPrincipalName value="FTSB01NB05\ASPNET" />
</identity>
</endpoint>
</client>
</system.serviceModel>



The interesting part lies in the client configuration file where the two bolded lines of text from top to bottom, represent the client port and the server port respectively. If you did not specify any of the address correctly you will probably receive the following error:

HTTP could not register URL http://+:80/client/ because TCP port 80 is being used by another application.

The error above indicates that you set your client port number the same port number as the server.

OR

There was no endpoint listening at http://localhost:8989/Services/StockService.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

The error above indicates that the server endpoint is specified incorrectly and that WCF failed to find the service.

The server configuration looks like the following

<system.serviceModel>

<behaviors>
<serviceBehaviors>
<behavior name="mexBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>

<services>

<service behaviorConfiguration="mexBehavior" name="EssentialWCF.StockService">
<endpoint address="" binding="wsDualHttpBinding" contract="EssentialWCF.IStockService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>