Click here to Skip to main content
Click here to Skip to main content

WCF Service Form Hosting and Duplex Communication

, 7 Dec 2008
Rate this:
Please Sign up or sign in to vote.
Replace your Remote Communcation with WCF.

Introduction

In this article, I am trying to explain the communication between two different WinForm applications using WCF. The attached solution has minimal code which demonstrates how the communication takes place between a WCF service and its clients.

For beginners, the best way to understand WCF projects is to avoid using Visual Studio’s auto generated Service, proxy classes. If you use Visual Studio, you end up adding a reference to the Service in your client, and VS will create the proxies for you. Adding a service reference to your client???? I think VS is wrong there.

Your client only needs to know about the contracts and proxies, no need to marry the service like Visual Studio does. The best practice is to create separate Contract, Service, and Proxy assemblies from scratch. Of course, you will end up with couple of extra assemblies; don’t worry, that’s what loose coupling is about.

Background

If you were using .NET Remoting to communicate between a WinForms server and a client, you might experience the necessity for bi-directional support and port availability issues. In this article, I will explain a WCF duplex communication and how simple it is to use.

Using the code

The contracts (interface) in this solution let a client to get and set a property using a WCF service. So, I define the contract as:

public interface IFormContracts
{

  [OperationContract]
  int GetHeight();

  [OperationContract]
  void SetHeight(int height);

}

I have created a WCF Service class FormService, implementing IFormContracts, which I am hosting in a WinForms ‘FormHost’. The host form is also a client of this service which subscribes the Get/Set behaviors of the service. The idea of the sample solution is to set a WinForms client(s)’s height using the track bar from the hosting form.

Any change you make in the track bar, i.e., SetHeight, will get notified to the registered clients of FormService. So, we need a mechanism to notify the WCF Service clients, and that’s what the “CallbackContract” is for. This contract tells your service that the clients who subscribes to you will provide a callback channel for notifications.

So, the interface declaration will be as follows, which says my callback contract interface is ‘ICallback’ and my clients will implement this interface:

[ServiceContract (SessionMode = SessionMode.Required, 
                  CallbackContract = typeof(ICallback))]

public interface IFormContracts
{

  [OperationContract]
  int GetHeight();

  [OperationContract]
  void SetHeight(int height);

}

Let's make a simple callback interface which notifies all clients upon a change in the ‘Height’ property of the service.

public interface ICallback
{
    [OperationContract(IsOneWay = true)]
    void NotifyCollection( int height);
}

So, how does the Service notify these clients? Well, it is not magic. When you connect to the service, provide some useful information to the Service to call you back, like your phone number. That’s where the synchronization context comes in to picture. How do you create a SynchornizationContext? It is easy, new InstanceContext(this);, where ‘this’ is the client object which implements the callback contract. Whenever you establish a connection with the Service, leave a client object instance in the synchronization context as a callback point, only one condition – your client should implement the callback contract.

So, the client proxy is the one who passes this information from the client to the service through the proper channel. Since we need to send and receive messages to and from the service, choose DuplexChannelFactory to create the communication.

In this solution, the client is a simple WinForms application which implements the callback contract. After it establishes the connection with the service, it will get notified by the service for any update you make on the host form’s track bar. Also, if you resize the height of the client form, it will notify the Service and the Service will notify its other clients. Here, it’s the host form itself. (That’s why the Host Form Configuration has entries for both Hosting and Consuming, as shown.)

<configuration>
  <system.serviceModel>
    <services>
      <service name="FormService.FormService">
       <endpoint address="net.tcp://localhost:8085/FormServiceEndPoint"
                 binding="netTcpBinding"
                 contract="contracts.IFormContracts" />
     </service>
   </services>
   <client>
     <endpoint address="net.tcp://localhost:8085/FormServiceEndPoint"
               binding="netTcpBinding"
               contract="contracts.IFormContracts" />
   </client>
 </system.serviceModel>
</configuration>

Points of interest

You can easily run into deadlock issues when you develop WCF solutions. Synchronization context is one area you should read about. Jual Lowy’s WCF book is a good place to start with.

There is a good blog entry where you will get an idea of why we need to use those different service attributes when we deal with WCF Duplex Communication: WCF Thread Synchronization Context.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Romy Sreedharan

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 Pinmembernilarka23-Jun-10 2:30 
QuestionCan the Host and Service be running on different machine on the same network and still work? PinmemberJohnny7925-Sep-09 13:02 
AnswerRe: Can the Host and Service be running on different machine on the same network and still work? Pinmemberromychayan26-Sep-09 13:27 
GeneralRe: Can the Host and Service be running on different machine on the same network and still work? PinmemberJohnny7927-Sep-09 17:43 
GeneralHello Pinmembermbaocha3-May-09 11:30 
GeneralTimeout Pinmemberantonio s simoes22-Dec-08 0:07 
GeneralRe: Timeout PinmemberRomy Sreedharan22-Dec-08 0:09 
GeneralRe: Timeout Pinmemberantonio s simoes22-Dec-08 0:48 
GeneralRe: Timeout [modified] Pinmemberantonio s simoes23-Dec-08 6:14 
GeneralRe: Timeout PinmemberRomy Sreedharan23-Dec-08 7:59 
GeneralRe: Timeout Pinmemberantonio s simoes31-Dec-08 3:15 
GeneralErrors PinmemberBydia7-Dec-08 21:49 
GeneralRe: Errors PinmemberRomy Sreedharan8-Dec-08 3:29 
GeneralRe: Errors PinmemberBydia10-Dec-08 1:39 
GeneralRe: Errors PinmemberRomy Sreedharan10-Dec-08 4:13 
GeneralRe: Errors PinmemberBydia11-Dec-08 1:58 
GeneralRe: Errors PinmemberRomy Sreedharan11-Dec-08 2:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 7 Dec 2008
Article Copyright 2008 by Romy Sreedharan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid