So this past weekend I spoke at Atlanta Code Camp this past Saturday on a WCF chat client. You can find the code samples here.
I thought I would expound on it here.
Start with the techniques you need to use,
For our chat client, we are using TCP/IP for communications because we are on a network, and want 2 way communication. TCP is great for this purpose, and there is a built in type of communications binding in WCF called netTCPBinding, which takes care of serialization of our types natively, since both the Server and Client are working with .Net object.
Here is the config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
Added by VS when configuration items are added.
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="ExampleService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
The important part of the WCF config file:
(See the inline comments)
<system.serviceModel>
<!-- Used to add Windows based security to the transport layer –>
This section defines Windows Security
<bindings>
<netTcpBinding>
<binding name="ExampleServiceChatBinding">
<security mode="Transport">
<transport clientCredentialType="Windows"/>
</security>
</binding>
</netTcpBinding>
</bindings>
<!-- Define the Services used here -->
<services>
<!-- behaviorConfiguration is used to provide the Metadata exchange listed below –>
This is where the endpoints are defined – one for mex and one for the actual chat service.
<service behaviorConfiguration="ExampleServiceBehavior"
name="ExampleService.ChatService">
<!-- define the endpoints
netTcpBinding - binding type allowing for 2-way communication using TCP/IP
contract - the class that is used for this binding
Binding Configuration - defines the above netTcpBinding settings for security
-->
The contract attribute matches to the [ServiceContract] attribute of the class that defines the class or interface to be used for the WCF service.
<endpoint binding="netTcpBinding"
contract="ExampleService.IChatService"
bindingConfiguration="ExampleServiceChatBinding"
/>
<!--
mex is used for Metadata Exchange, allowing for a proxy class to be
built on the client side.
binding - mexTcpBinding is built-in for Metadata TCP/IP binding
contract - IMetadataExchange is the built-in interface for Metadata TCP/IP binding
-->
This endpoint is optional, but recommended. This will provide the ability to generate a proxy class on the client side.
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
<host>
<!--
Defining a baseAddress for the host, allows for this single URI to be applied for all endpoints.
-->
<baseAddresses>
<add baseAddress="net.tcp://ATLCODECAMP/services/ChatService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
This section is required if you plan to use the MEX.
<serviceBehaviors>
<!-- Required for Metadata Exchange to work. -->
<behavior name="ExampleServiceBehavior">
<serviceMetadata />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<applicationSettings>
</applicationSettings>
</configuration>
That is it for the configuration file (app.config). The nice thing about WCF is that this can all be configured via XML, but can also be defined in code.
Next post is the Interface and derived class.