The use of message queue is the last appearance in the book's WCF asynchronous technology. Message Queue can provide persistence, reliability, and transaction for message transmission. Even the client that sends the message Program Services that receive messages do not need to run at the same time. However, using this flexibility requires a certain price, that is, message queues can only support one-way transmission. Therefore, if you use this technology to implement client programs and services and the service needs to send response messages to the client, you need to carefully design them. In addition, message queues are slower than other transmission protocols because they support reliability and sustainability. In Windows, messages need to be stored on disks. This means that messages in the message queue are not affected by shutdown or power failure. However, this robustness requires additional I/O costs when creating and transmitting messages. Although the MSMQ programming model is very simple, you may have used MSMQ to build a Message Queue program, but you still need to pay attention to it. This is because the MSMQ technology you use in WCF is fundamentally different from the traditional C/S program. However, one goal of WCF is to maintain consistency when sending and receiving messages, regardless of which transmission protocol is used at the underlying layer of WCF, therefore, message queue-based WCF is similar to other transmission protocols. However, the message queue used by WCF is different from the Message Queue Technology you used in the past. In the last series of exercises in this chapter, you will see that it is very easy to implement one-way asynchronous operations using message queue as the transmission protocol. Exercise: Use message queue to implement the WCF Service 1. use Visual Studio to open * \ WCF \ step. by. step \ solutions \ chapter12 MSMQ solution 2. in the solution browser window, open iservice under the adventureworksadminhost project. CS file this file defines an iadventureworksadmin interface. This interface contains a single operation generatedailysalesreport. Note that this operation also indicates that this operation is a one-way operation through the isoneyway attribute of the operationcontract feature class. This setting is very important because all operations of the services implemented through message queue must be unidirectional. 3. Open the service. CS file under the adventureworksadminhost project. This file contains the adventureworksadmin class that implements the iadventureworksadmin interface. Note that the generatedailysalesreport method is only waiting for 10 seconds (simulating the report generation process) and then displaying a message dialog box to indicate that the method is complete. 4. Open the hostcontroller. XAML file in the design window. Check the file and you will find that the file is used to host the adventurewokrsadmin service. use the WCF Service configuration editor to open the app. config file 6. in the configuration Panel of the WCF configuration Editor, right-click the service and choose create service 7. in the right pane, enter adventureworksadmin8in the name attribute field. In the configuration panel, right-click the endpoint folder under the adventureworksadmin service, and select create service endpoint 9. on the right panel, set the properties of the endpoint according to the following table.
Attribute |
Value |
Name |
Adventureworksadminmsmqendpoint |
Address |
Net. MSMQ: // localhost/private/adventureworksadmin |
Bind |
Netmsmqbinding |
Service Contract |
Iadventureworksadmin |
The message queue URI consists of net. MSMQ and queue name. MSMQ identifies queues in a way similar to HTTP URLs, although it is somewhat different from HTTP in academic sense. The "private" section of the URL indicates that it is a private message queue, which means that the queue can only run on a local computer. If your computer is located in the Windows domain, you can create a public message queue so that all computers in the domain can access this message queue. The real name of the Message Queue we created is adventureworksadmin.
10. On the configuration panel, right-click the binding folder and select a new binding configuration. In the create binding configuration dialog box, click the netmsmqbinding type, and then click OK.
11. On the right panel, set the binding configuration name to adventureworksadminmsmqbindingconfig. You can set the binding attribute to control many aspects of Message Queue work. For example, the durable attribute determines whether a message can still exist without the impact of processing failure, shutdown, or restart. setting this attribute to false will cause the message to be vulnerable to destruction. The exactlyonce attribute involves message reliability transmission of MSMQ. setting this attribute to true ensures that the message is received once and only once, and the message is not lost or accidentally accepted twice by the concurrent instance. If this attribute is set to true, the message queue must be transactional (when you create a message queue on the console, you can specify whether a queue is a transactional queue ). 12. click the security label and modify the security mode attribute of the binding to none message queue. Message-level security and transmission-level security are supported, although transport-Level Security is unique to MSMQ, you are not required to configure SSL. If you implement message-level security, you can specify the client certificate type. You should note that the MSMQ message-level security authentication mechanism requires configuring the Message Queue Server, which must provide a certificate for the message queue used for binding. 13. on the configuration panel, click adventureworksadminmsmqendpoint endpoint definition. On the right panel, set adventureworksadminmsmqbindingconfig14. Save the configuration file and exit the WCF Service configuration editor 15. in Visual Studio, generate the adventureworksadminhost project. Experiment: Send messages from a WCF client to a message queue. open the Visual Studio command line tool window and go to * \ WCF \ step. by. step \ solutions \ chapter12 \ MSMQ \ adventureworksadminhost \ bin \ debug folder, and then enter the following command to generate the client proxy: svcutil adventureworksadminhost.exe svcutil/namespace: *, adventureworksadmintestclient. adventureworksadmin adventure-works.com.2010.07.01.wsdl *. XSD/out: adventureworksadminproxy. CS is generated by the proxy class Code Included in the adventureworksadminproxy. CS file. Note that when the WCF Service uses the MSMQ transfer protocol, you cannot use the add service reference wizard that comes with Visual Studio to add the WCF Service. 2. Return to Visual Studio and add the adventureworksadminproxy. CS file to the adventureworksadmintestclient project. 3. open programm under the adventureworksadmintestclient project. CS file. The main method of this file calls the generatedailysalesreport method of the service twice. 4. use the WCF Service configuration editor to edit the app of the adventureworksadmintestclient project. config File 5. on the configuration panel, expand the client folder, right-click the endpoint, and choose create client endpoint. 6. On the client endpoint panel, set the properties of the endpoint using the following table:
attribute |
value |
name |
msmqbinding_adventureworksadmin |
address |
net. MSMQ: // localhost/private/adventureworksadmin |
bind |
netmsmqbinding |
service contract |
adventureworksadmintestclient. adventureworksadmin. administrativeservice |
7. Add a binding configuration based on the netmsmqbinding type. Set the binding name to adventureworksadminmsmqbindingconfig. Modify the binding security mode to none8. return to the msmqbinding_adventureworksadmin endpoint definition, and set the bindingconfiguration attribute value to adventureworksadminmsmqbindingconfig9. Save the configuration file and exit the WCF Service configuration editor 10. in programm. in the CS file, modify the statement for creating a proxy object: So far, you have completed all the code for the service and client programs. Next, you need to create a message queue and test the above services. Experiment: Create an adventureworksadmin queue and test the service. 1. Click the Start Menu of windows, right-click my computer, and click manage to start the computer management console. 2. in the computer management console, expand the service and program nodes in the left-side Navigation Pane, expand the Message Queue nodes, right-click the private queue, and select create a new queue, finally, select create private queue. 3. In the create private queue dialog box, enter adventureworksadmin In the Name field, select transactional support, and click OK. 4. Do not close the Computer Management Console and return to Visual Studio. 5. Run the solution in non-adaptive mode. In the console window of the client, press enter to send two generatedailysalesreport messages to the service. Note that the service has not been started yet, but the client has successfully sent the two messages. Press enter to close the console window of the client. 6. Go back to the computer management console and expand the adventurworksadmin queue under the Private queue. You will find two messages displayed on the right panel. 7. In the adventureworks admin host window, click Start to start the service, and then obtain and process messages from the message queue in sequence. After the message is processed, a message box is displayed for service operations. When the second message dialog box appears, click the stop button to stop the service and close the adventureworks admin host window. 8. Go back to the computer management console. You will find that the two messages displayed in Step 6 have disappeared. 9. Shut down the computer management console. MSMQ provides an easy-to-use mechanism for asynchronous operations. However, netmsmqbinding limits only one-way operations. If a service needs to send a response, the Service asynchronously sends a message to a queue that a client program can connect. This involves implementing different messages for each client and associating messages to each client. Only in this way can the client program know which Response Message corresponds to which request message.