August 23, 2011

Net MSMQ Service

1.       Create Non-Transaction private MSMQ – TestNetMsmq
2.       Add Dto:
namespace TestNetMsmq
{
    public class TestMessageDto
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

3.       Create Service interface as below:
using System.ServiceModel;

namespace TestNetMsmq
{
    [ServiceContract(Name = "ITestService", Namespace = "urn://MyTest/Service/TestService/2011/03")]
    public interface ITestService
    {
        [OperationContract(IsOneWay = true)]
        void PersistLogMessage(TestMessageDto message);
    }
}
4.       Create Implementation as below
using System;

namespace TestNetMsmq
{
    public class TestService : ITestService
    {
        public void PersistLogMessage(TestMessageDto message)
        {
            Console.WriteLine(message.Name);
        }
    }
}

5.       Make sure to change .svc file as below:
<%@ ServiceHost Language="C#" Debug="true" Service="TestNetMsmq.TestService" CodeBehind="TestService.svc.cs" %>
6.       Change Web.config file as below
xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <netMsmqBinding>
        <binding name="NetMsmqBinding_TestService" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" exactlyOnce="false">
          <security mode="None"/>
        </binding>
      </netMsmqBinding>
      </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="TestBehaviors">
         
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
         
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
    <services>
      <service name="TestNetMsmq.TestService" behaviorConfiguration="TestBehaviors">
        <endpoint address="net.msmq://localhost/private/TestNetMsmq" binding="netMsmqBinding" bindingConfiguration="NetMsmqBinding_TestService" contract="TestNetMsmq.ITestService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
  
  </system.serviceModel>
  <system.webServer>
   
   
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
 
</configuration>

7.       Build the project
8.       For the service, In IIS add ‘http,net.msmq’ as enabled protocols in advances settings
9.       Use following in web.config file to keep service alive all the time; this may not work on local m/c
      
   

10.   For Net.Msmq binding you cannot add message directly in Q;
11.   Create console project; Add service refrence to above
12.   Add following code to call MSMQ service
using ConsoleTestNetMsmq.ServiceReference1;

namespace ConsoleTestNetMsmq
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceReference1.TestServiceClient test = new TestServiceClient();
            test.PersistLogMessage(new TestMessageDto { Id = 1, Name = "Abcd" });
        }
    }
}




Key points:
1.       For the service, In IIS add ‘http,net.msmq’ as enabled protocols in advances settings
2.       For Net.Msmq binding you cannot add message directly in Q;
3.       Use following in web.config file to keep service alive all the time; this may not work on local m/c
     
   

4.       Message in MQ  is not readable & shouldn’t be tampered