Creating MSMQ Integration Binding Service with 3 different formatters
Add Service TestMsmqIntegration
Add Sample Object as below
Add Web.Config as below
Next add the following test client in Console program
Note that to send object you also need sample object at client side
Important points to note:
- Binary Array - UTF 8 or UTF 16 messages
- Object
- XML
First add 3 non-transnational Message Queue's named below
- testnetmsmq
- testnetmsmqObject
- testnetmsmqXElement
Add TestMsmqIntegration interface as below
- using System;
- using System.ServiceModel;
- using System.ServiceModel.MsmqIntegration;
- using System.Xml.Linq;
- namespace TestMsmqIntegration
- {
- // you can use one service (.svc) file however you need three separate interfaces
- [ServiceContract(Namespace = "urn://MyTest/IntBindingService/ITestService/2013/11")]
- public interface IMsmqIntService
- {
- [OperationContract(IsOneWay = true, Action = "*")]
- [ServiceKnownType(typeof(Byte[]))]
- void ProcessByteArrayMessage(MsmqMessage<Byte[]> mqMessage);
- }
- [ServiceContract]
- public interface IMsmqIntServiceObject
- {
- // USING OBJECT
- [OperationContract(IsOneWay = true, Action = "*")]
- [ServiceKnownType(typeof(SampleObject))]
- void ProcessObjectMessage(MsmqMessage<SampleObject> mqMessage);
- }
- [ServiceContract]
- public interface IMsmqIntServiceXElement
- {
- // USING XElement
- [OperationContract(IsOneWay = true, Action = "*")]
- [ServiceKnownType(typeof(XElement))]
- void ProcessXElementMessage(MsmqMessage<XElement> mqMessage);
- }
- }
- using System;
- using System.Diagnostics;
- using System.ServiceModel.MsmqIntegration;
- using System.Text;
- using System.Xml.Linq;
- namespace TestMsmqIntegration
- {
- public class MsmqIntService : IMsmqIntService, IMsmqIntServiceObject, IMsmqIntServiceXElement
- {
- // byte[]> can also be used to get UTF-16 XML messages
- public void ProcessByteArrayMessage(MsmqMessage<byte[]> mqMessage)
- {
- Debug.WriteLine("in1");
- XElement xResponse = null;
- var responseMessageBody = mqMessage.Body;
- string responseMessageString;
- //load utf-16 message
- try
- {
- responseMessageString = Encoding.Unicode.GetString(responseMessageBody);
- xResponse = XElement.Parse(responseMessageString);
- }
- catch (Exception ex)
- {
- //UTF-16 failed - try for UTF-8
- responseMessageString = Encoding.UTF8.GetString(responseMessageBody);
- xResponse = XElement.Parse(responseMessageString);
- }
- // process message received from MQ
- Console.WriteLine(xResponse.ToString());
- Debug.WriteLine(xResponse.ToString());
- }
- public void ProcessObjectMessage(MsmqMessage<SampleObject> mqMessage)
- {
- SampleObject so = (SampleObject)mqMessage.Body;
- Debug.WriteLine(so.Name);
- }
- public void ProcessXElementMessage(MsmqMessage<XElement> mqMessage)
- {
- XElement so = (XElement)mqMessage.Body;
- Console.WriteLine(so.Value);
- }
- }
- }
- using System;
- namespace TestMsmqIntegration
- {
- [Serializable]
- public class SampleObject
- {
- public int Id { get; set; }
- public string Name { get; set; }
- }
- }
- <?xml version="1.0"?>
- <configuration>
- <system.web>
- <customErrors mode="Off" />
- <compilation debug="true" targetFramework="4.5" />
- <httpRuntime targetFramework="4.5" />
- </system.web>
- <system.serviceModel>
- <behaviors>
- <serviceBehaviors>
- <behavior>
- <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
- <serviceDebug includeExceptionDetailInFaults="true"/>
- </behavior>
- </serviceBehaviors>
- </behaviors>
- <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
- <services>
- <service name="TestMsmqIntegration.MsmqIntService" >
- <!--End point config to read message as byearray-->
- <endpoint name="ByteArraySvc" address="msmq.formatname:DIRECT=OS:.\private$\testnetmsmq"
- binding="msmqIntegrationBinding" bindingConfiguration="IntQBind_ByteArray"
- contract="TestMsmqIntegration.IMsmqIntService"/>
- <!--End point config to read message as object-->
- <endpoint name="ObjectSvc" address="msmq.formatname:DIRECT=OS:.\private$\testnetmsmqObject"
- binding="msmqIntegrationBinding" bindingConfiguration="IntQBind_Object"
- contract="TestMsmqIntegration.IMsmqIntServiceObject"/>
- <!--End point config to read message as XML-->
- <endpoint name="XElementSvc" address="msmq.formatname:DIRECT=OS:.\private$\testnetmsmqXElement"
- binding="msmqIntegrationBinding" bindingConfiguration="IntQBind_Xelement"
- contract="TestMsmqIntegration.IMsmqIntServiceXElement"/>
- <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
- </service>
- </services>
- <bindings>
- <!--use exactlyOnce="true" if MQ is transactional-->
- <!--binding config to read message as Byte array-->
- <msmqIntegrationBinding>
- <binding name="IntQBind_ByteArray" exactlyOnce="false" maxReceivedMessageSize="6553600" serializationFormat="ByteArray">
- <security mode="None"/>
- </binding>
- <!--binding config to read message as object-->
- <binding name="IntQBind_Object" exactlyOnce="false" maxReceivedMessageSize="6553600" >
- <!--Note serializationFormat="Binary" won't work so do not specify any serializationFormat for object -->
- <security mode="None"/>
- </binding>
- <!--binding config to read message as XML-->
- <binding name="IntQBind_Xelement" exactlyOnce="false" maxReceivedMessageSize="6553600" serializationFormat="Xml">
- <security mode="None"/>
- </binding>
- <!--More details on msmqmessageserializationformat-->
- <!--https://msdn.microsoft.com/en-us/library/system.servicemodel.msmqintegration.msmqmessageserializationformat(v=vs.110).aspx-->
- </msmqIntegrationBinding>
- </bindings>
- </system.serviceModel>
- <system.webServer>
- <!--Following config makes sure to init service automatically; may not work in local env-->
- <!--<applicationInitialization doAppInitAfterRestart="true">
- <add initializationPage="/MsmqIntService.svc"/>
- </applicationInitialization>-->
- <modules runAllManagedModulesForAllRequests="true"/>
- <directoryBrowse enabled="true"/>
- </system.webServer>
- </configuration>
- using System.Messaging;
- namespace TestMQConsole
- {
- class Program
- {
- static void Main(string[] args)
- {
- SendByteArray(@"<TestMQConsole>AJIT</TestMQConsole>", @".\private$\testnetmsmq");
- SendObject(new SampleObject { Id = 1, Name = "Ajit" }, @".\private$\testnetmsmqobject");
- SendXElement(@"<TestMQConsole>AJIT</TestMQConsole>", @".\private$\testnetmsmqxelement");
- }
- public static void SendByteArray(object body, string queueName)
- {
- using (var queue = new MessageQueue(queueName))
- {
- var logMessage = new Message { Body = body };
- queue.Send(logMessage);
- }
- }
- public static void SendObject(SampleObject so, string queueName)
- {
- using (var queue = new MessageQueue(queueName))
- {
- var logMessage = new Message(so);
- queue.Send(logMessage);
- }
- }
- public static void SendXElement(string xml, string queueName)
- {
- using (var queue = new MessageQueue(queueName))
- {
- var logMessage = new Message { Body = xml };
- queue.Send(logMessage);
- }
- }
- }
- }
- using System;
- namespace TestMQConsole
- {
- [Serializable]
- public class SampleObject
- {
- public int Id { get; set; }
- public string Name { get; set; }
- }
- }
- You can use one service (.svc) file however you need three separate interfaces
- Remove comments from Web.Config file for the following code to activate service automatically so it can continuously listen MQ;
- Use exactlyOnce="true" in web.config file (binding) if MQ is transactional
- byte[] can also be used to get UTF-16 XML messages Below is Web.config file w/o encoding