Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » WCF » A Simple Duplex Service in WCF

A Simple Duplex Service in WCF

This article will give step by step explanation on how to create a simple duplex service in WCF.

Author Rank :
Page Views : 8197
Downloads : 289
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
WcfService3.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Objective

This article will give step by step explanation on how to create a simple duplex service in WCF.

In call back operation or duplex service, Service can also call some function at the client .

Duplex1.gif

  1. Duplex service allows calling back some operation (function) on the client.
     
  2. Duplex service also knows as Call Backs. 
     
  3. All Binding does not support duplex service. 
     
  4. Duplex communication is not standard. They are absolute Microsoft feature. 
     
  5. wsDualHttpBinding supports duplex communication over HTTP binding. 
     
  6. Duplex communication is possible over netTcpBinding and netNamedPipeBinding

Steps involved in creating a duplex service

  1. Create a WCF Service
     
  2. Create a call back contract. 
     
  3. Create service contract 
     
  4. Implement service and configure the behavior for duplex communication and call the client function at the service. 
     
  5. Configure the endpoint for duplex service 
     
  6. Create the client. 
     
  7. Call the WCF Service

Create a WCF Service

  1. Open visual studio and create a new project. Select WCF Service application from WCF project template tab.
     
  2. Delete all the default generated code. 
     
  3. If using VS2010, open web.config and comment serviceHostEnvironment as below. 

    Duplex2.gif
     
  4. If using VS2008 then delete the default End Point generated. 
     
  5. Now you are left with empty IService1 and Service1.svc.cs file.

Create a call back contract

  1. Open IService1.cs.
     
  2. Add an interface in the same file. Give any name of the interface. I am giving name here IMyContractCallBack 

    Duplex3.gif
     
  3. Create an operation contract in the Interface.
     
  4. Make sure return type is Void. 
     
  5. Make sure IsOneWayProperty is set to true. 
     
  6. There is no need to mark call back interface as ServiceContract.

Explanation

At the client side, this call back interface will get implemented and on a duplex channel Service will call the CallBackFunction from the client.

Create service contract

  1. Open IService1.
     
  2. Declare operation contract called from the client. Make the isoneway to property to true from the service operation contract also. 

    Duplex4.gif
     
  3. Set the CallBackContract in Service contract attribute. Set the call back contract as the interface created for the call back.

    Duplex5.gif

Here IMyContractCallBack is name of the interface user for duplex communication.

So Service contract and call back contract will be as below,

IService1.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;


namespace
WcfService3

{

    [ServiceContract(CallbackContract=typeof(IMyContractCallBack))]

    public interface IService1

    {

        [OperationContract(IsOneWay=true)]

        void   NormalFunction();

    } 

    public interface IMyContractCallBack

    {

        [OperationContract(IsOneWay=true)]

        void    CallBackFunction(string str);

    }

}

Implement service and configure for duplex communication and call client function

  1. In service behavior set the instance mode of the service to percall.

    Duplex6.gif
     
  2. Create a call back channel

    Duplex7.gif

    Here I am using OperationConetxt to get current call back channel.
     
  3. Now instance of IMyContractCallBack can be used to make call at the function in call back interface contract at the client side. Like below ,

    Duplex8.gif
Here we are going to call function at the client side from the operation contract of the service.

Service1.svc.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;


namespace
WcfService3

{  

    [ServiceBehavior(InstanceContextMode= InstanceContextMode.PerCall)]

    public class Service1 : IService1

    {  

        public void     NormalFunction()

        {

            IMyContractCallBack callback = OperationContext.Current.GetCallbackChannel<IMyContractCallBack>();       

            callback.CallBackFunction("Calling from Call Back");

        }

    }

}

Configure the endpoint for duplex service

As we discussed earlier that duplex service is not supported by all type of bindings. So while configuring the end point for the service we need to choose the binding supports duplex communication. Here we are going to use wsDualHttpBinding

  1. Declare the end point with wsDualHttpBinding.

    Duplex9.gif
     
  2. Declare the Meta data exchange end point. 

    Duplex10.gif
     
  3. Declare the host address 

    Duplex11.gif
     
  4. Declare the service behavior

    Duplex12.gif

So the service in configuration will look like

Duplex13.gif

Web.Config

<?xml version="1.0"?>

<configuration>

  <system.web>

    <compilation debug="true" targetFramework="4.0" />

  </system.web>

  <system.serviceModel>

    <behaviors>

      <serviceBehaviors>

        <behavior name ="svcbh">        

          <serviceMetadata httpGetEnabled="False"/>        

          <serviceDebug includeExceptionDetailInFaults="False"/>

        </behavior>

      </serviceBehaviors>

    </behaviors>

    <!--<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />-->

    <services>

      <service name ="WcfService3.Service1" behaviorConfiguration ="svcbh" >

        <host>

          <baseAddresses>

            <add baseAddress = "http//localhost:9000/Service1/" />

          </baseAddresses>

        </host>

        <endpoint name ="duplexendpoint"

                  address =""

                  binding ="wsDualHttpBinding"

                  contract ="WcfService3.IService1"/>

        <endpoint name ="MetaDataTcpEndpoint"

                  address="mex"

                  binding="mexHttpBinding"

                  contract="IMetadataExchange"/>

      </service>

    </services>

  </system.serviceModel>

 <system.webServer>

    <modules runAllManagedModulesForAllRequests="true"/>

  </system.webServer

</configuration>

Create the client

  1. Create a console application to consume this service.
     
  2. Add the reference of System.ServiceModel. In console application project. 
     
  3. Add the service reference of the service, we created in previous steps.

Create a duplex proxy class to implement call back contract

  1. Right click and add a class in console application. Give any name; I am giving the name MyCallBack.
     
  2. Add the namespace 

    Duplex14.gif
     
  3. Implement call back contract. In our case it is IService1Callbackand IDisposable.

    Duplex15.gif

    Note: Since name of the service contract is IService1, so the call back interface name will be IService1Callback . It is name of service contract suffixes by keyword CallBack.
     
  4. Now implement the call back contract method in this class. 

    Duplex16.gif
     
  5. Now make a function in this class to create duplex proxy.

    a. Create an instance of InstanceContext and pass the reference of current class in the constructor of that.

    Duplex17.gif

    b. Pass this context as parameter of service1client

    Duplex18.gif

    c. Call the service on this proxy

    Duplex18.1.gif

So, the function will be

Duplex19.gif


For your reference MyCallback class will be as below,

MyCallback.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using ConsoleApplication1.ServiceReference1;

using System.ServiceModel;


namespace
ConsoleApplication1

{

    class MyCallBack :IService1Callback ,IDisposable

    {

        Service1Client proxy;    

        public void   CallBackFunction(string str)

        {

            Console.WriteLine(str);

        }

        public void callService()

        {

           InstanceContext context = new InstanceContext(this);

           proxy = new Service1Client(context);

           proxy.NormalFunction();

        }

        public void Dispose()

        {

            proxy.Close();

        }

    }

}

Call the WCF Service

Create the instance of MyCallBack class and call the callService() method to call the service.

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using ConsoleApplication1.ServiceReference1;

using System.ServiceModel;


namespace
ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            MyCallBack obj = new MyCallBack();

            obj.callService();

            Console.Read();

            obj.Dispose();

        }

    }

}

Output

Duplex20.gif 

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Dhananjay Kumar
Dhananjay Kumar is a developer who blogs at http://debugmode.net/. He is Microsoft MVP ,Telerik MVP and Mindcracker MVP. You can follow him on twitter  @debug_mode
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
Greate example by Mike On December 1, 2010
Thanks, it was a very helpful sample program.
Reply | Email | Modify 
Problem by hitesh On December 21, 2010
i have face problem in this implementation while running this console application.it give me a AddressAlreadyUseException...i dont know what is actual problem....please help me..
Reply | Email | Modify 
Findout error by sANKATI On January 12, 2012
Sir I have executed above Program CallBackContract (Which should be same name in the client and server side) Since it is Declared in the Server side and Client Side Implementation your using in Server side IMyContractCallBack and Your implementing at client side as IService1CallBack
Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.