Using Message Queues


Message queues are the ultimate in asynchronous technology. Message queues can provide a durable, reliable, transacted transport for messages. Furthermore, a client application sending messages, and a service receiving them, do not have to be running at the same time. There is a price you pay for this flexibility though: message queues are an inherently one-way transport, so implementing applications and services that send requests and expect to receive responses requires a lot of careful design. Message queues are also slower than other transports, primarily because of their reliability and durability; the Windows operating system stores messages in files on disk. This means that messages held in a message queue can survive machine shutdown and power failure, at the cost of the additional I/O involved in creating and transmitting them.

Note 

You can specify that messages are not durable if performance is more important than reliability. So-called volatile messages are cached in memory rather than disk and consequently do not survive machine restarts or crashes.

If you have already built message queuing applications using Microsoft Message Queue (MSMQ), you will appreciate that although the programming model is straightforward, it is fundamentally different compared to the programming practices you adopt when building a more traditional client/server application. However, one of the goals of WCF is to provide a consistent model for sending and receiving irrespective of the underlying transport, so using message queues with WCF is very similar to using most other transports, but is somewhat different from the message queuing techniques you might have used in the past.

In the final set of exercises in this chapter, you will see just how easy it is to use message queues as a transport for asynchronous one-way operations.

Implement a WCF service that uses message queuing

  1. Using Visual Studio 2005, open the solution file image from book AdventureWorksAdmin.sln located in the Microsoft Press\WCF Step By Step\Chapter 11\MSMQ folder under your \My Documents folder.

    This solution contains two projects: AdventureWorksAdminHost, which is a self-hosted version of the AdventureWorksAdmin WCF service, and AdventureWorksAdminTestClient, which is a client application for testing the service.

  2. In Solution Explorer, open the file image from book Service.cs in the AdventureWorksAdminHost project.

    This is the code that defines and implements the service contract. It should look familiar, as it is very similar to the service you created in the first set of exercises in this chapter. The service contains a single operation, GenerateDailySalesReport. Notice that the operation contract still specifies that this is a OneWay operation. This is important as all operations in a service accessed through a message queue must be OneWay operations. Also note that the implementation of the GenerateDailySalesReport method now only waits for 10 seconds (pretend that you are running on a faster machine than before, so the processing takes less time).

  3. Open the image from book HostController.xaml file. This is a version of the Windows Presentation Foundation form that you previously used to host the ProductsService service.

  4. Open the code behind this form in the file image from book HostController.xaml.cs. The logic in this form is the same as before. The only difference is that this form now hosts the AdventureWorksAdmin service.

  5. Add a new application configuration file to the AdventureWorksAdminHost project, and then edit this file by using the WCF Service Configuration Editor.

  6. In the WCF Service Configuration Editor, create a new service and set the Name property to AdventureWorksAdmin (this is the name of the class implementing the service).

  7. Add an endpoint to this service. Set the Name property to AdventureWorksAdminMsmqEndpoint, set the Address property to net.msmq://localhost/private/AdventureWorksAdmin, set the Binding property to netMsmqBinding, and set the Contract property to IAdventureWorksAdmin.

    The format for a message queuing URI consists of the scheme “net.msmq” followed by the name of the queue. MSMQ identifies queues using a syntax very similar to HTTP URLs, although the semantics are a little different. The “private” part of the URI indicates that this is a private message queue, meaning that it can only be accessed from applications running on the local computer. If you are using a computer that is a member of a Windows domain, you can also create public message queues that can be accessed by code running on other computers. The actual name of the message queue is “AdventureWorksAdmin.”

    More Info 

    For a detailed description of message queues, see the topic “Using Messaging Components” in the Microsoft Windows SDK documentation, and also on the Microsoft Web site at http://msdn2.microsoft.com/en-us/library/fzc40kc8.aspx.

  8. Add a new binding configuration using the netMsmqBinding binding type. Set the Name property of this binding configuration to AdventureWorksAdminMsmqBindingConfig.

    You can set binding properties that control many aspects of the way the message queue works. For example, the Durable property determines whether messages should be capable of surviving process failure or machine shutdown and restart; setting this property to False makes messages volatile. The ExactlyOnce property is the MSMQ analog of reliable messaging for other transports. Setting this property to True guarantees that messages will be received once and once only; messages will not be lost, or inadvertently retrieved twice by concurrent instances of the service from the message queue. Setting this property to True requires that the message queue is transactional.

  9. Modify the security settings of the binding configuration, and set the Mode property to None.

    Message queues support message level security and transport level security, although the implementation of transport level security is peculiar to MSMQ and does not require you to configure SSL. If you implement message level security, you can specify the client credential type. You should note that the authentication mechanism implemented by MSMQ message level security requires that the message queue server must be configured to provide a certificate for the message queue used by the binding.

    Important 

    For simplicity, this example uses a local, unprotected private message queue that is accessible only on the host computer. In a production environment, you will probably use public queues, which should be protected by using transport or message level security.

  10. Return to the AdventureWorksAdminMsmqEndpoint endpoint definition you added in step 7. Set the BindingConfiguration property to AdventureWorksAdminMsmqBinding Config.

  11. Save the configuration file, and exit the WCF Configuration Editor.

  12. In Visual Studio 2005, build the AdventureWorksAdminHost project. Do not try and build the entire solution, as the client application is not yet complete; this is your next task.

Send messages to a message queue in a WCF client application

  1. Open a Microsoft Windows SDK CMD Shell window, and move to the Microsoft Press\WCF Step By Step\Chapter 11\MSMQ\AdventureWorksAdminHost\bin\Debug folder under your \My Documents folder. Type the following commands to generate the client proxy from the service contract compiled into the AdventureWorksAdminHost.exe assembly:

     svcutil AdventureWorksAdminHost.exe svcutil /namespace:*,AdventureWorksAdminTestClient.AdventureWorksAdmin adventure- works.com.2007.01.01.wsdl *.xsd /out:AdventureWorksAdminProxy.cs

  2. Return to Visual Studio 2005, and add the image from book AdventureWorksAdminProxy.cs file that you have just created to the AdventureWorksAdminTestClient project.

  3. Open the image from book Program.cs file in the AdventureWorksAdminTestClient project. Again, this code should look very familiar, as it is almost identical to the client application you developed in the first set of exercises in this chapter for testing OneWay operations. There is an additional prompt “Press ENTER to send messages” in the try block, and you must specify a binding to use when instantiating the proxy. Before you do this, you must create an application configuration file and define the binding you are going to use.

  4. Add a new application configuration file to the AdventureWorksAdminTestClient project and edit it by using the WCF Service Configuration Editor.

  5. In the WCF Service Configuration Editor, add a new client endpoint with the properties shown in the following table:

    Open table as spreadsheet

    Property

    Value

    Name

    AdventureWorksAdminMsmqEndpoint

    Address

    net.msmq://localhost/private/AdventureWorksAdmin

    Binding

    netMsmqBinding

    Contract

    AdventureWorksAdminTestClient.AdventureWorksAdmin. AdministrativeService

  6. Add a binding configuration based on the netMsmqBinding type. Set the Name property of this binding configuration to AdventureWorksAdminMsmqBindingConfig. Change the security settings of the binding configuration, and set the Mode property to None.

  7. Return to the AdventureWorksAdminMsmqEndpoint endpoint definition and set the BindingConfiguration property to AdventureWorksAdminMsmqBindingConfig.

  8. Save the configuration file and exit the WCF Service Configuration Editor.

  9. In the image from book Program.cs file, modify the statement that creates the proxy object and replace the text “INSERT ENDPOINT HERE” with the name of the MSMQ endpoint, as shown in bold below:

     AdministrativeServiceClient proxy =     new AdministrativeServiceClient("AdventureWorksAdminMsmqEndpoint");

That completes the code. You can now create the message queue and test the client application and service.

Create the AdventureWorksAdmin queue and test the service

  1. On the Windows Start menu, right-click My Computer and then click Manage.

    The Computer Management console starts.

  2. In the Computer Management console, expand the Services and Applications node in the left pane, expand the Message Queuing node, right-click the Private Queues folder, point to New, and then click Private Queue.

  3. In the New Private Queue dialog box, type AdventureWorksAdmin in the Queue name text box, check the Transactional box, and then click OK.

    Note 

    If you don’t want the overhead of transactional message queues, you must set the ExactlyOnce property of the binding configuration for the netMsmqBinding binding to False.

  4. Leave the Computer Management console open, and return to Visual Studio 2005.

  5. Start the solution without debugging. In the client console window, press Enter to send the two GenerateDailySalesReport messages but don’t start the service running yet. Notice that the client successfully sends the messages even though the service is not running. Press Enter to close the client console window.

  6. Return to the Computer Management console. Expand the AdventureWorksAdmin queue in the Private Queues folder under Message Queuing, and then click the Queue messages folder. Two messages should be displayed in the right pane:

    image from book

    Tip 

    If no messages appear, click Refresh on the Action menu to update the display.

    If you double-click a message, you can display its properties, including the text in the body of the message.

  7. In the AdventureWorksAdminHost form for the service host application, click Start.

    The service starts running, retrieves each message from the queue in turn, and processes them (remember that each message takes at least 10 seconds to process). The operation displays a message box after each message is processed.

    Stop the service and close the AdventureWorksAdminHost form after the second message box has been displayed.

  8. Return to the Computer Management console displaying the messages in the message queue. On the Action menu, click Refresh to update the display. Both messages should disappear as they have now been removed from the message queue by the WCF service.

    Close the Computer Management console.

MSMQ provides an easy-to-use mechanism for implementing asynchronous operations. However, the netMsmqBinding binding restricts you to implementing OneWay operations. If a service needs to send a response, it can do so asynchronously by sending a message to a queue to which the client application can connect. This involves implementing a different message for each client (for privacy) and correlating messages, so the client application knows which response corresponds to which request.

More Info 

For more information and an example of using message queues to implement asynchronous request/response messaging, see the topic “Two-Way Communication” in the Windows SDK documentation, which is also available on the Microsoft Web site at http://windowssdk.msdn.microsoft.com/en-us/library/ms752264.aspx.




Microsoft Windows Communication Foundation Step by Step
Microsoft Windows Communication Foundation Step by Step (Step By Step Developer Series)
ISBN: 0735623368
EAN: 2147483647
Year: 2007
Pages: 105
Authors: John Sharp

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net