Introduction:
In this article, i am going to explain about how to use netmsmq binding in wcf
and how to create message queue programming using wcf.
Main:
MSMQ offers support for building distributed applications using queues.WCF supports
communication through MSMQ queues as the underlying transport for the netMsmq binding.
The netMsmqBinding binding allows clients to post messages directly to queue and services
to read messages from a queue.There is no direct communication between the client and
server,therefore the communication is inherently disconnected.It also means that all
communication must be one-way.Therefore,all operations must have the IsOneWay=true
property set on the operation contract.
See this below demonstration,
Before starting demonstration first we need to check the microsoft msmq option is
enabled in yours machine.If not please enable it,

Please check is there any private queue allready created,(If not you can create it here
itself or you can crete it through coding),
Now we need to create a wcf application for msmq service and we to host it,
Goto File — New — Project — select windows tab — select console application
and named it MsmqDemo (See the below picture)

Copy and paste the below code in Program.cs,
using System;
using System.Configuration;
using System.Messaging;
using System.ServiceModel;
namespace MsmqDemo
{
// Define a service contract.
[ServiceContract]
public interface IEmpService
{
[OperationContract(IsOneWay = true)]
void GetEmpReport(int Empid, string EmpName, int Salary);
}
// Service class which implements the service contract.
// Added code to write output to the console window
public class EmpService : IEmpService
{
[OperationBehavior]
public void GetEmpReport(int Empid, string EmpName, int Salary)
{
Console.WriteLine("Employee Details - Employee Id:{0},EmployeeName:{1},EmployeeSalary:{2}",
Empid.ToString(), EmpName.ToString(),
Salary.ToString());
Console.WriteLine();
}
// Host the service within this EXE console application.
public static void Main()
{
// Get MSMQ queue name from app settings in configuration
string queueName = ConfigurationManager.AppSettings["queueName"];
// Create the transacted MSMQ queue if necessary.
if (!MessageQueue.Exists(queueName))
MessageQueue.Create(queueName, true);
// Get the base address that is used to listen for
// WS-MetaDataExchange requests
// This is useful to generate a proxy for the client
string baseAddress = ConfigurationManager.AppSettings["baseAddress"];
// Create a ServiceHost for the EmpService type.
using (ServiceHost serviceHost = new ServiceHost(
typeof(EmpService), new Uri(baseAddress)))
{
serviceHost.Open();
Console.WriteLine("The Trade Service is online.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHost to shutdown the service.
serviceHost.Close();
}
}
}
}
using System; using System.Configuration; using System.Messaging; using System.ServiceModel; namespace MsmqDemo { // Define a service contract. [ServiceContract] public interface IEmpService { [OperationContract(IsOneWay = true)] void GetEmpReport(int Empid, string EmpName, int Salary); } // Service class which implements the service contract. // Added code to write output to the console window public class EmpService : IEmpService { [OperationBehavior] public void GetEmpReport(int Empid, string EmpName, int Salary) { Console.WriteLine("Employee Details - Employee Id:{0},EmployeeName:{1},EmployeeSalary:{2}", Empid.ToString(), EmpName.ToString(), Salary.ToString()); Console.WriteLine(); } // Host the service within this EXE console application. public static void Main() { // Get MSMQ queue name from app settings in configuration string queueName = ConfigurationManager.AppSettings["queueName"]; // Create the transacted MSMQ queue if necessary. if (!MessageQueue.Exists(queueName)) MessageQueue.Create(queueName, true); // Get the base address that is used to listen for // WS-MetaDataExchange requests // This is useful to generate a proxy for the client string baseAddress = ConfigurationManager.AppSettings["baseAddress"]; // Create a ServiceHost for the EmpService type. using (ServiceHost serviceHost = new ServiceHost( typeof(EmpService), new Uri(baseAddress))) { serviceHost.Open(); Console.WriteLine("The Trade Service is online."); Console.WriteLine("Press <ENTER> to terminate service."); Console.WriteLine(); Console.ReadLine(); // Close the ServiceHost to shutdown the service. serviceHost.Close(); } } } } |
Next right click solution and add new item and select application Configuration file and paste the
below code,
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="queueName" value=".\private$\EmpQueue"/>
<add key="baseAddress" value="net.msmq://localhost/private/EmpQueue"/>
</appSettings>
<system.serviceModel>
<services>
<service
behaviorConfiguration="MyServiceTypeBehaviors"
name="MsmqDemo.EmpService">
<endpoint address="net.msmq://localhost/private/TradeQueue"
binding="netMsmqBinding"
bindingConfiguration="DomainlessMsmqBinding"
contract="MsmqDemo.IEmpService"
/>
<!-- Add the following endpoint. -->
<!-- Note: your service must have an http base
address to add this endpoint. -->
<endpoint contract="IMetadataExchange" binding=
"mexHttpBinding" address="mex" />
</service>
</services>
<behaviors>
<serviceBehaviors >
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled ="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netMsmqBinding>
<binding name="DomainlessMsmqBinding" >
<security>
<transport
msmqAuthenticationMode="None"
msmqProtectionLevel="None"/>
</security>
</binding>
</netMsmqBinding>
</bindings>
</system.serviceModel>
</configuration>
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="queueName" value=".\private$\EmpQueue"/> <add key="baseAddress" value="net.msmq://localhost/private/EmpQueue"/> </appSettings> <system.serviceModel> <services> <service behaviorConfiguration="MyServiceTypeBehaviors" name="MsmqDemo.EmpService"> <endpoint address="net.msmq://localhost/private/TradeQueue" binding="netMsmqBinding" bindingConfiguration="DomainlessMsmqBinding" contract="MsmqDemo.IEmpService" /> <!-- Add the following endpoint. --> <!-- Note: your service must have an http base address to add this endpoint. --> <endpoint contract="IMetadataExchange" binding= "mexHttpBinding" address="mex" /> </service> </services> <behaviors> <serviceBehaviors > <behavior name="MyServiceTypeBehaviors"> <serviceMetadata httpGetEnabled ="true"/> </behavior> </serviceBehaviors> </behaviors> <bindings> <netMsmqBinding> <binding name="DomainlessMsmqBinding" > <security> <transport msmqAuthenticationMode="None" msmqProtectionLevel="None"/> </security> </binding> </netMsmqBinding> </bindings> </system.serviceModel> </configuration> |
Now Build it and run
so now we hosted the wcf application,
Next create one client application,
Goto File — New — Project — select windows tab — select console application
and named it MsmqClient (See the below picture)

Next we need to create a proxy for client application,for creating proxy
please goto .Net commandline and type the below
svcutil net.msmq://localhost/private/EmpQueue /out:myproxy.cs /config:app.config
Now paste the below code in client application,
using System;
using System.Data;
using System.Messaging;
using System.Configuration;
using System.Web;
using System.Transactions;
namespace MsmqClient
{
class Client
{
static void Main()
{
// Create a proxy for the client
using (TradeServiceClient proxy = new TradeServiceClient())
{
//Create a transaction scope.
using (TransactionScope scope = new TransactionScope
(TransactionScopeOption.Required))
{
Console.WriteLine("Seniour Manager Details");
proxy.GetEmpReport("100", "Peter", "$6000");
Console.WriteLine("Assistant Manager Details");
proxy.GetEmpReport("98", "Michelle", "$3000");
// Complete the transaction.
scope.Complete();
}
}
}
}
}
using System; using System.Data; using System.Messaging; using System.Configuration; using System.Web; using System.Transactions; namespace MsmqClient { class Client { static void Main() { // Create a proxy for the client using (TradeServiceClient proxy = new TradeServiceClient()) { //Create a transaction scope. using (TransactionScope scope = new TransactionScope (TransactionScopeOption.Required)) { Console.WriteLine("Seniour Manager Details"); proxy.GetEmpReport("100", "Peter", "$6000"); Console.WriteLine("Assistant Manager Details"); proxy.GetEmpReport("98", "Michelle", "$3000"); // Complete the transaction. scope.Complete(); } } } } } |
Now build it and run,
Thatsit…
Conclusion:
Hope this helps,
Happy coding.

well written blog. Im glad that I could find more info on this. thanks
What a great resource!
I have build one console application for service using your code and i’ve run the application then getting some exception like below
Could not find a base address that matches scheme http for the endpoint with binding MetadataExchangeHttpBinding. Registered base address schemes are [net.msmq].
Could you please help me out in this.
Hi Sama,
See the below in app.config
if you are using net.msmq endpoint then its not needed,
Good article and it works fine.
messages gets queued to server
now think of payment situation where client submitted the payment to server using msmq bining
next thing my server should respond back to client that ok your payment has been processed.
so it would be like reading the messages from server and respond back
so how to read those messages..
any help!
Ashish,just adapt duplex concept,once the payment is processed raise the callback and inform client,for details about duplex please visit my another article “http://netprogramminghelp.com/wcf/how-to-createperform-duplex-service-contracts-in-wcf/’
Hi Lingareddy
i too have same problem,i solved my problem,
write instead of
I totally agree with everything you have mentioned. Actually, I browsed through your various other blogposts and I do think that you’re certainly right. Congrats with this particular website.