In this article you are going to see how to use Message Queuing in WCF.
Windows Communication Foundation provides support for queued calls using NetMsmqBinding. Instead of transporting a message over TCP or HTTP, Windows Communication Foundation transports the message over Microsoft Message Queuing (MSMQ). Instead of sending the Windows Communication Foundation message to a live service, the client posts the message to an MSMQ queue. All the client sees and interacts with is the queue, not a service endpoint. As a result, the calls are inherently asynchronous and disconnected. The calls will execute when the service processes the messages at some time in the future.
Above paragraph is from http://msdn.microsoft.com/en-us/magazine/cc163482.aspx
Lets go through this application Step by Step
In this application, we are using console application. Open VS 2008>New>Project>Console Application>choose your programming Language, here i am using c# but you can use VB>Give the name of the project as MSMQ Server(This is our Server)>OK
Make sure that you have installed Message Queuing. If not use your windows OS, Go to control Panel>Add or Remove Programs>Add Windows Component>There you can see Message Queuing>Install Message Queuing.
Now in the MSMQ Server, give a Reference to System.ServiceModel.
Now the time has come to write our Service contract,operation contract. Here i am write simple operation Contract. where it prints just "HI".Where, I am concentrating more on configuration part.
Here you can see in the OperationContract, i set IsOneWay=true why because this NetMsmqBinding only supports one way communication. But it is different where you can use Duplex or Request-reply communication. For that You need to customize the things. But in this application, We are just looking at one way contract.
Here you can see the endpoint address like net.msmq://ahosur1/private/anup which is of format net.msmq://machine name/private/queuename. In this we are setting the security to None. One more thing is appsettings, there in the value filed i gave the path of the Message Queue. IMPORTANT thing here in endpoint address of netmsmq you should specify the queue path. Otherwise it will tell like whether you installed Message Queue properly or not.
Now write the service host and some other things. First you give reference to System.Messaging.
Run the server and check any error if any fix those.
Now we are using a tool called svcutil.
You need to type in VS command Prompt like this
svcutil /out:proxy.cs /config:app.config http://localhost:8003/Service
It will generate proxy.cs and app.config.
Now lets create a client application. go to File>New>Project>ConsoleApp>give the name as MSMQ client>OK
Give the Reference to System.ServiceModel.Copy and paste the proxy.cs and app.config in to the client app.
Now write the client like this
iserviceClient is noting but the class name of the proxy.cs.
Now let this application. In this case we are just running the client 3 times.
Now check the Message queue.
Start>run>compmgmt.msc>services and application>message queue>under private queue>my queuename is anup it may be different in your application>queue message.
Here you can see 3 messages in the message queue.
Now lets run Server
Here you can see 3 hi messages.
Now lets got to message queue again and refresh that screen u can see blank screen.
I hope you people like this application.
Reference:Standard definition of Queued Calls has been taken exactly from http://msdn.microsoft.com/en-us/magazine/cc163482.aspx and credit goes to this article.
About the Author