Sukasom C. Go! Home Tutorial WCF And MSMQ Part 1 August 18, 2008 Messaging technology allows a client and a server to send a message to each other, and they do not have to run at the same time. One party can send a message to another party at anytime. A crash on one side does not effect the another side. In this tutorial, I am going to implement a simple WCF client-server application without any consideration on security. We are going to use MSMQ as a transport mechanism. I use Microsoft Visual Studio 2008. Please read Queuing in WCF msdn page for further information. Note: you have to install MSMQ on your computer. Please refer to this msn page for what is WCF (Windows Communication Foundation) and what does it solves. Please refer to this msdn page for various WCF technical term. Each WCF service has an endpoint. An endpoint consists fo address, binding and contract. - An address is usually a URI which specifies where the endpoint of the service is. - A binding defines the transport mechanism between a client and a server. There are some predefined bindings avaiable. In this tutorial, we are going to use MSMQ as a transport mechanism, so we are interested in NetMSMQBinding. - A contract is your service, and it is implemented in C# in this tutorial. Steps: 1. Create a server project Create a server project by simply select a C# console application from “New Project” dialog. Let’s name the solution name as “wcf_tutorial” and the server project name as “WCFServer1.” New project and new solution dialog in Visual Studio 2008 Please refer to “How to Add or Remove References in Visual Studio” msdn page for how to add a reference. You need to add two references which are System.messaging (found in .NET tab when opening up the “Add Reference” dialog) and System.ServiceModel.dll (found in “C:\WINDOWS\Microsoft.NET\Framework\v3.0\Windows Communication Foundation” in Windows folder, click on “Browse” tab in the “Add Reference” dialog). 2. Create server source files IWork is an interface which will be configure as a contract in a binding configuration. WorkService is the implementation of this contract. The server does nothing. It just receives a string from a client. Please refer to How to: Define a Windows Communication Foundation Service Contract msdn page for how define a contract. Please remember than a MSMQ is a messaging technology which is anynchronous communication. There is no respond back from a server like web service. Thus, the function has no return type which is “void.” - Please refer to Build a Queued WCF Response Service for more information on WCF and MSMQ. - Please refer to How to: Implement a Windows Communication Foundation Service Contract msdn page for how to implement a contract. //////Program.cs////////////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.Messaging; using System.Configuration; namespace WCFServer1 { [ServiceContract(Namespace = "http://sukasom.wordpress.com")] interface IWork { [OperationContract(IsOneWay = true)] void submitWork(String str1); } public class WorkService : IWork { public void submitWork(String str1) { Console.WriteLine(“Server has received a work : “ + str1); } } class Program { static void Main(string[] args) { string queueName = ConfigurationSettings.AppSettings["queueName"]; if (!MessageQueue.Exists(queueName)) MessageQueue.Create(queueName, true); using (ServiceHost serviceHost = new ServiceHost(typeof(WorkService))) { serviceHost.Open(); Console.WriteLine(“The service is ready.”); Console.WriteLine(“Press <ENTER> to terminate service.”); Console.ReadLine(); } } } } Put the above code in the Program.cs. The server part is modified from How to: Host a WCF Service in a Managed Application msdn page. 3. Configure server configuration file You need to configure the configuration file. Add an App.config file into your project. Please refer to Specifying an Endpoint Address msdn page for more information on address configuration. My configuration file is modified from NetMSMQBinding msdn page. Configuration of WCF service using MSMQ as a transport mechanism ///////////////App.config ///////////////////////// <?xml version=”1.0” encoding=”utf-8” ?> <configuration> <appSettings> <add key=”queueName” value=”.\private$\ServiceModelSamples” /> </appSettings> <system.serviceModel> <services> <service name=”WCFServer1.WorkService” behaviorConfiguration=”Behavior1“> <endpoint address=”net.msmq://localhost/private/ServiceModelSamples“ binding=”netMsmqBinding” bindingConfiguration=”test“ contract=”WCFServer1.IWork” /> </service> </services> <!– Following is necessary for getting information about the service through http with the svcutil.exe tool. –> <behaviors> <serviceBehaviors> <behavior name=”Behavior1“> <serviceMetadata httpGetEnabled=”true” httpGetUrl=”http://localhost:8000/Hello/” /> </behavior> </serviceBehaviors> </behaviors> <!– For first example, the security is just disabled. –> <bindings> <netMsmqBinding> <binding name=”test“> <security mode=”None“> </security> </binding> </netMsmqBinding> </bindings> </system.serviceModel> </configuration> In the “service” element, an endpoint is defined with an address, a binding and a contract. This WCF sample application uses MSMQ as a transport mechanism, so the binding is the NetMsmqBinding. The contract specifies the interface which is WCFServer1.IWork. The security is turned off in this tutorial through the bindingConfiguration named “test.” A client needs to be able to access the metadata of the service, so it is defined through the behaviorConfiguration named “Behavior1″ where the httpGetEnabled attribute is set to true. It means that the metadata can be retrieved through the http through the URL set in the httpGetUrl attribute which is “http://localhost:8000/Hello/” in this tutorial. This URL allows the client to get a metadata about the service from the correct location. You can open up the “http://localhost:8000/Hello/“ on the browser as shown below. Metadata on the service More info on address and httpGetEnabled can be found in WCF Part 6 : Address blog. 4. Implement the client project. Start a visual studio 2008 and add a new C# console project as shown in the figure below. The name of the project is “WCFClient1.” Just like the server part, add the reference to System.ServiceModel.dll. More info on accessing service through a client at Accessing Services Using a WCF Client msdn page. Add new project into an existing solution in microsoft visual studio 2008 5. Generate a client proxy. The ServiceModel Metadata Utility Tool (Svcutil.exe) is needed to generate the proxy file for a client. The proxy file allows you to treat a remote service as a local service. You simply instantiate a remote object and call a function on it. This is much simple compared with some old distributed technologies. Please refer to ServiceModel Metadata Utility Tool (Svcutil.exe) msdn page for the manaul of Svcutil.exe. Please start the server first then type the following command in the command prompt. “C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\SvcUtil.exe” http://localhost:8000/Hello/ The URI is the URI that is defined in the App.config in the server part. After running the above command, you will get two files which are WorkService.cs and output.config. Generating a proxy file for a WCF client project using SvcUtil.exe Please rename the output.config to App.config. Copy those two files into your project directory, and add those files into your project. Following is the code for the client: ////////////Program.cs in the client part///////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WCFClient1 { class Program { static void Main(string[] args) { WorkClient wc = new WorkClient(); wc.submitWork(“hello work 1″); } } } Calling a server is very simple, instantiating the proxy object and calling a function on it. You can see that there is a class called WorkClient defined in the WorkService.cs. Please run the server first. If the queue has not been created, it will be created. When the queue is created, you don’t have to run the server before running the client. You can run the client first, and the message from the client will be stored in the queue. The message will be sent to the server when the server is started again. Server part You can take a look Tom Hollander’s blog for another good tutorial on this topic. Here is the complete source code. The book from [Lowy] is also good. Possibly related posts: (automatically generated) Another WCF Example using wsDualHttpBinding Tales from the WCF webHttpBinding crypt… Windows Communication Foundation The WCF Run-Down Ads by Google WCF Test Client: WCFStorm WCF Functional & Load Testing tool Also works with Web Services! www.wcfstorm.com Entry Filed under: Distributed application, Microsoft and .NET, wcf. Tags: msmq, wcf. 1 Comment Add your own 1. Another WCF Example using wsDualHttpBinding « Sukasom | August 19, 2008 at 2:57 pm [...] Posts Unit testing with Eclipse and JUnitWCF and MSMQ part 1Flashover and BackdraftLevel of disaster in Germany (MANV)How to get [...] Reply Leave a Comment Name Required Email Required, hidden Url Comment 238 0 Submit 1257343098 Notify me of follow-up comments via email. Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> Trackback this post | Subscribe to the comments via RSS Feed Categories database (1) Distributed application (6) Education (1) emergency response (3) entrepreneurship (9) firefighter (Feuerwehr) (5) Flex/Flash (2) German Language (Deutsch) (8) Java (8) Microsoft and .NET (12) Risk Management (Disaster) (1) Software Testing (7) Uncategorized (9) User Interface Design and Technology (7) wcf (3) Windows Workflow (3) Top Posts Deutsch Lesson (verbs which take Dativ and Akkusativ) Why do I want a Mac computer Multithreading in C# Windows Form Unit testing with Eclipse and JUnit Some Error messages when trying out WCF WCF And MSMQ Part 1 Obtaining connection string from visual studio ABAP/4 Programming for SAP book SAP BW/SEM online course Recent Posts ABAP/4 Programming for SAP book Celebrity Apprentice II: Wedding Dresses Why do I want a Mac computer Learn German from VW ads Lost in Translation Search for: Search Pages Tutorial Archives October 2009 July 2009 June 2009 April 2009 October 2008 September 2008 August 2008 July 2008 June 2008 May 2008 April 2008 March 2008 February 2008 January 2008 November 2007 December 2006 August 2008 M T W T F S S « Jul Sep » 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Blogroll Albrecht Schmidt – User Interface Engineering Blognone, news about IT (Thai language) Dolores Joya Moore, User Interface Design and Flex\Flash Ed Dunhill, Microsoft Education PR ERMA – Electronic Risk Management Architecture ezybzy.info Health (Thai language) Jonas Landgren, Information Technology and Emergency Response Jusci, science in Thai Michal Migurski, User Interface Design Science and Technology Scholarships ScottGu’s Blog, .NET Using Visio for UML Class Diagram Valleywag, Silicon Valley Tech Books Software Testing And Analysis: Process, Principles And Techniques Sun Certified Programmer & Developer for Java 2 Study Guide The data warehouse lifecycle toolkit Blog Stats 9,687 hits Top Clicks msdn.microsoft.com/en-us/… sap.wip.uni-due.de/live/E… winfobase.de/lehre/wb_mat… - Category Cloud database Distributed application Education emergency response entrepreneurship firefighter (Feuerwehr) Flex/Flash German Language (Deutsch) Java Microsoft and .NET Risk Management (Disaster) Software Testing Uncategorized User Interface Design and Technology wcf Windows Workflow Spam Blocked 3,330 spam comments blocked by Akismet Theme: Blix by Sebastian Schmieg . Blog at WordPress.com.