Monday, April 29, 2013

WCF- Queuing Service

In distributed application design use of direct transport such as   TCP or HTTP service, client, and the network must be running at the same time for the application to work.
In Queued transports provide isolation, which means that if the service or client fail or if communication links between them fail, the client and service can continue to function.


MSMQ stands for Microsoft Message Queuing is one of the most reliable ways to sending and receiving messages from one system to another system.

 Messages are stored in the queue of the system (Microsoft Operating System) and once the receiving application runs, it starts peeking the messages one by one.

A single MSMQ message can contain multiple WCF messages, or just a single one, according to the contract session mode.

 In effect, instead of sending the WCF message to a live service, the client posts the message to an MSMQ queue. All that the client sees and interacts with is the queue, not a service endpoint. As a result, the calls are inherently asynchronous (because they will execute later, when the service processes the messages) and disconnected (because the service or client may interact with local queues).

Binding used  :  NetMsmqBinding

Queued Calls Architecture


Queued Contracts :

·         WCF disallows using fault contracts on queued operations

·         NetMsmqBinding can have only one-way operations   


Example : We are creating service to send email notification

Step 1 :   Installing MSMQ Server

Go to Add remove program ->Turn Windows feature ON/OFF (Window 7)


MSMQ should be installed on Client machine  as well.

Step 2.  Creating the Queue

è Right click  on Computer

è Select Manage It will open computer Management window

è Expand the 'Services and Applications'  

è Expand 'Message Queuing' node and right click the private queues node and select 'New'->'Private Queue'. Make sure transactional check box will be checked


Interface :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

public interface IEmailNotify

    [OperationContract(IsOneWay = true)]
    void SendEmail(string value);

Service :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Diagnostics;
using System.Data.SqlClient; 

    public class EmailNotification : IEmailNotify
        public void SendEmail(string value)
                    MailMessage mm = new MailMessage();
                    mm.From = new MailAddress("");
                    mm.To.Add(new MailAddress(""));
                    mm.Subject = "Alert";
                    mm.IsBodyHtml = true;
                    mm.Body = "Hi" + value;
                    SmtpClient smtp = new SmtpClient();
            catch (Exception ex)



WebConfig File :
<?xml version="1.0"?>

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

  <!-- Email Host setting-->
      <smtp deliveryMethod="Network">
        <network host="" userName="abc" password="abcb"/>

        <binding name="MsmqBindingNonTransactionalNoSecurity" exactlyOnce="true">
          <security mode="None"/>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

      <service behaviorConfiguration="MyServiceBehavior" name="EmailNotify">


          contract="IEmailNotify" />



        <behavior name="MyServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />



Right click on service and view in browser

Client :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace ClientApplication
    class Program
        static void Main(string[] args)

            ServiceReference1.EmailNotifyClient obj = new ServiceReference1.EmailNotifyClient();
            obj.SendEmail("USer 1 ");
            obj.SendEmail("USer 2 ");


Note: Make sure service should be running before run client application.

OutPut :

1 comment :

  1. the problem of MSMQ is client has to enable/msmq service should be run at client also?