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 

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;


 [ServiceContract]
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)
        {
            try
            {          
                    MailMessage mm = new MailMessage();
                    mm.From = new MailAddress("AlertService@Mailservice.com");
                    mm.To.Add(new MailAddress("User1@vammsl.com"));
                    mm.Subject = "Alert";
                    mm.IsBodyHtml = true;
                    mm.Body = "Hi" + value;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Send(mm);                 
              
            }
            catch (Exception ex)
            {

            }



        }
    }


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


  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>


  <!-- Email Host setting-->
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network">
        <network host="mail.Host.com" userName="abc" password="abcb"/>
      </smtp>
    </mailSettings>
  </system.net>
 
 
  <system.serviceModel>

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

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <services>


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

        <endpoint
          address="net.msmq://localhost/private/testqueue"

          binding="netMsmqBinding"
          bindingConfiguration="MsmqBindingNonTransactionalNoSecurity"
          contract="IEmailNotify" />


      </service>


    </services>

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

  </system.serviceModel>

</configuration>


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 ");
            Console.ReadLine();        

         
        }
    }
}


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?

    ReplyDelete