Learn WCF (11) with me -- queue service details in WCF, and wcf queue
I. Introduction
In the previous WCF Service, both the service and the client must be started and run to implement interaction between them. However, in many cases, we hope that a service-oriented application can have offline interaction capabilities. WCF uses the service queue method to support offline work between the client and the service. The client sends messages to a queue and then the service processes them. Next let's take a look at the queue service in WCF.
Ii. Advantages of the WCF Queue Service
Before introducing the WCF queue service, you must first understand Microsoft Message Queue (MSMQ ). MSMQ is an asynchronous transmission mode for communication between multiple different applications. Applications that communicate with each other can be distributed on the same machine or in a connected network environment. The implementation principle is that the client sends a Message to a container and saves it to the Message Queue in a public space of the system, the local or remote service then obtains the messages sent to it from the queue for processing. For more details, refer to my blog: Learn WCF (1) -- MSMQ Message Queue with me.
The WCF framework integrates and extends MSMQ. MSMQ supports the offline message mode and provides call extensions for the internet network queue service based on the http Bridge in the WCF framework. This gives the WCF queue service the following advantages:
Iii. Communication Framework of the WCF Queue Service
WCF uses NetMsmqBinding to support Message Queue communication. When a client calls a service, the client message is encapsulated as an MSMQ message and sent to a common message queue. The service host starts a channel listener in the running state to detect messages in the message queue, if a message is found, the message is retrieved from the queue and forwarded to the corresponding service by the distributor. The specific communication framework is shown in:
1 [ServiceContract] 2 public interface IWCFMSMQService 3 {4 // operation contract, which must be one-way operation 5 [OperationContract (IsOneWay = true)] 6 void SayHello (string message ); 7} 8 9 // Contract implementation 10 public class WCFMSMQService: IWCFMSMQService11 {12 public WCFMSMQService () 13 {14 Console. writeLine ("wcf msmq Service instance was created at: {0}", DateTime. now); 15} 16 17 # region IOrderProcessor Members18 19 [OperationBehavior (Trans ActionScopeRequired = true, TransactionAutoComplete = true)] 20 public void SayHello (string message) 21 {22 Console. WriteLine ("Hello! {0}, the time for calling the WCF operation is: {1} ", message, DateTime. Now); 23} 24 25 # endregion26}
Note the preceding code: a WCF operation must be defined as a one-way operation, because a queue service is implemented, which is asynchronous and offline and has no return value. Therefore, set the IsOneWay attribute to true.
Step 2: implement the host. The console application is still used as the host of the WCF queue service. The specific implementation code is as follows:
1 namespace WCFConsoleHost 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 string path = @".\private$\LearningHardWCFMSMQ"; 8 if (!MessageQueue.Exists(path)) 9 {10 MessageQueue.Create(path, true);11 }12 13 using (ServiceHost host = new ServiceHost(typeof(WCFMSMQService)))14 {15 host.Opened += delegate16 {17 Console.WriteLine("Service has begun to listen\n\n");18 };19 20 host.Open();21 22 Console.Read();23 }24 }25 }26 }
The corresponding configuration information is as follows:
<Configuration> <system. serviceModel> <behaviors> <serviceBehaviors> <behavior name = "inherit"> <serviceMetadata httpGetEnabled = "true"/> </behavior> </serviceBehaviors> </behaviors> <bindings> <netMsmqBinding> <binding name = "msmqBinding"> <security> <transport msmqAuthenticationMode = "None" msmqProtectionLevel = "None"/> <message clientCredentialType = "None"/> </security> </binding> </netMs MqBinding> </bindings> <services> <service behaviorConfiguration = "msmqServiceBehavior" name = "WCFContractAndService. WCFMSMQService "> <endpoint address =" net. msmq: // localhost/private/LearningHardWCFMSMQ "binding =" netMsmqBinding "bindingConfiguration =" msmqBinding "contract =" WCFContractAndService. IWCFMSMQService "/> <! -- End of Publishing Service metadata --> <endpoint address = "mex" binding = "mexHttpBinding" contract = "IMetadataExchange"/>
Step 3: implement the WCF client. Start the host as an administrator, and then generate a proxy client class by adding a service reference. In the address bar of the add service reference window, enter: http: // localhost: 9003/mex to add a service reference. After a service reference is successfully added, a proxy class is generated to access the WCF Service through the proxy class. The specific implementation code is as follows:
1 namespace WCFClient 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 WCFMSMQServiceClient proxy = new WCFMSMQServiceClient ("NetMsmqBinding_IWCFMSMQService "); 8 using (TransactionScope scope = new TransactionScope (transactionscospontion. required) 9 {10 Console. writeLine ("WCF First Call at: {0}", DateTime. now); 11 proxy. sayHello ("World"); 12 13 Thread. sleep (2000); // The client Sleep for two seconds and continues to call the 14 Console next time. writeLine ("WCF Second Call at: {0}", DateTime. now); 15 proxy. sayHello ("Learning Hard"); 16 17 scope. complete (); 18} 19 20 Console. read (); 21} 22} 23}
After the above three steps, we have completed a WCF queue service program. Next let's take a look at the running results of the program.
Since the WCF queue service is integrated and extended to MSMQ, the program client can also run offline in the case of the WCF Service, that is, the WCF Service host is not started, the client can also run normally, which is different from the previously described WCF program because the previous WCF program directly starts the client program if the host program is not started, when the client is running, an exception occurs. The reason why this program does not cause exceptions is that the client program interacts directly with the message queue instead of the WCF Service. Run only the WCF client. You will see the running result as shown in:
All source code in this article: WCFMSMQService.zip