WCF distributed development step for win: WCF service offline operation with Message Queuing MSMQ

Source: Internet
Author: User
Tags msmq

Previously wrote an article about MSMQ Message Queuing: WCF Distributed Development Prerequisite Knowledge (1): MSMQ Message Queuing, which was also used as the basis for learning WCF Message Queuing MSMQ programming. In that article, we described in detail the basic concepts of MSMQ Message Queuing, installation, deployment, development, debugging, and other related issues. Today we are going to learn the WCF distributed development step for win: WCF service offline operation with Message Queuing MSMQ.  Programming with the MSMQ Message Queuing service under the WCF framework. Here I'll give you a demo sample program that uses WCF MSMQ to implement offline requests.

Full-text structure is: "1" MSMQ basic concept "2" benefits of WCF Message Queuing MSMQ "3" WCF Message Queuing MSMQ communication framework "4" Installation Configuration Considerations "5" Sample Code "summary"

"1" Basic concepts of MSMQ:

Briefly review the basic concepts of MSMQ, in detail you can refer to WCF Distributed development Prerequisites (1): MSMQ Message Queuing.

The MSMQ full name, Microsoft Message Queue, is an asynchronous transfer mode that communicates with multiple different applications, which can be distributed on the same machine or in any location in the connected network space. The idea is that the sender of the message puts the information he wants to send into a container (what we call a message) and then saves it to a message queue in a system's public space A local or offsite message receiver is then taken out of the queue to process messages sent to it.

Benefits of the "2" WCF Message Queuing MSMQ:

The benefits of Message Queuing MSMQ: stability, message prioritization, offline capabilities, and security, guaranteed messaging, and reliable fail-safe mechanisms for performing many business processes. Therefore, Message Queuing is one of the important components to implement SOA service-oriented architecture. The WCF framework provides the ability to integrate and scale with MSMQ. This is also what WCF explicitly points out in the feature. MSMQ supports offline message mode and, under the WCF framework, provides a call extension for an Internet network queue service based on an HTTP bridge. The combination and extension of the MSMQ framework makes the WCF service a new feature:

"2.1" Availabiliy: Availability. This is an embodiment of MSMQ offline messaging. The customer order and the server do not need to connect in real time and then interact with the message. A WCF client can send a request to the offline service side, and the service goes online after the corresponding client request.

"2.2" disjoint: decomposition. It can be said that work is broken down into multiple operations, one in a queue. Improve system availability and throughput.

"2.3" Compensating: compensation. For multi-service transactions, it is possible to provide separate things to provide for the failure of other transactions to deal with the aftermath.

"2.4" Load leveling: Load balancing. Overloaded client requests can be queued, processed during idle time, balance system throughput, and improve performance.

"3" WCF Message Queuing MSMQ communication framework:

WCF uses netmsmqbinding to support Message Queuing communication. When the client invokes the service, the client message is encapsulated as an MSMQ message that is sent to the specific message queue. The server host will start the channel listener to detect Message Queuing messages under the running turntable, and if the corresponding message is found, the message will be fetched from the queue and forwarded to the corresponding service using the dispatcher. The specific communication architecture

If the host is offline, the message is placed in the queue, waiting for the next time the host is online, to perform message distribution processing to the specified WCF service.

"4" Installation Configuration considerations:

Several common types of MSMQ queues are:

1. Public queues: replicated throughout the Message Queuing network and may be accessed by all sites that are connected by the network.
2. Private queues: Not published throughout the network. Instead, they are available only on the local computer where they reside. A private queue can only be accessed by an application that knows the full path name or label of the queue.
3. Manage queues: Contains messages confirming the receipt of messages sent in a given Message Queuing network. Specifies the management queue (if any) that you want the MessageQueue component to use.
4. Response queue: Contains the response message returned to the sending application when the message is received by the target application. Specifies the response queue, if any, that you want the MessageQueue component to use.

Here are a few questions to note that many people have encountered this problem when configuring the MSMQ development environment. There are many limitations to MSMQ configuration development under the XP environment. Here is a summary for everyone to refer to:

1. The public queue requires a DC domain controller for the DCS;
2. The private queue is local to the managed machine and does not require a DC to be installed as a workgroup;
3. Private queues need to disable Safe mode: Workgroup installation and security, security settings require clients to provide certificates, MSMQ transport security requires Windows security, and ad Active Directory is used here.

"5" Sample code:

Today's sample demo program code, mainly demonstrates how WCF configures and develops an MSMQ service program that implements WCF offline operations, and the part of the MSMQ transaction is not covered for the time being because of the large content. We only discuss WCF offline operations.

(1) WCF Service code:

Notable WCF operations are defined as one-way operations, because they are inherently consistent with the one-way operation characteristics. asynchronous, offline. no return value. Add the IsOneWay property when configuring the Operation contract. The code is as follows:

1. Service Contract
[ServiceContract (Namespace = "http://www.cnblogs.com/frank_xl/")]
public interface Iwcfmsmqservice
{
Operation contract, must be one-way operation
[OperationContract (IsOneWay = True)]
void Sayhellomsmq (string name);

}
2. Service class, inherit interface. Implementing the operations defined by the service contract
public class Wcfmsmqservice:iwcfmsmqservice
{
Public Wcfmsmqservice ()
{
Console.WriteLine ("WCF MSMQ Service instance was created at:{0}", DateTime.Now);
}
Ways to implement an interface definition
public void Sayhellomsmq (string name)
{
Console.WriteLine ("Hello! {0},calling WCF MSMQ Service Operation At:{1} ", Name,datetime.now);

}
}

(2) Host:

My development environment is XP Professional Edition, workgroup mode. Here's a question to be aware of, which is safety. The default security mode for MSMQ is to require certificate support. We must configure none in the host configuration file to simplify the operation because the certificate requires the MSMQ domain manager. MSMQ transport security requires Windows security, which requires ad Active Directory support, which is not supported in workgroup mode, so here we are all set to none. The code is as follows:

<services>
<service behaviorconfiguration= "Wcfservice.wcfservicebehavior"
Name= "Wcfservice.wcfmsmqservice" >
<endpoint
Address= "NET.MSMQ://LOCALHOST/PRIVATE/FRANKWCFMSMQ"
Binding= "NetMsmqBinding"
contract= "Wcfservice.iwcfmsmqservice" bindingconfiguration= "MSMQ" >
</endpoint>

<endpoint address= "Mex" binding= "mexHttpBinding" contract= "IMetadataExchange"/>
<baseAddresses>
<add baseaddress= "http://localhost:8001/"/>
</baseAddresses>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name= "Wcfservice.wcfservicebehavior" >
<servicemetadata httpgetenabled= "true"/>
<servicedebug includeexceptiondetailinfaults= "false"/>
</behavior>

</serviceBehaviors>
</behaviors>
<bindings>
<netMsmqBinding>
<binding name= "MSMQ" durable= "false" Exactlyonce= "false" >
<security mode= "None" >
<transport msmqprotectionlevel= "None"/>
<message clientcredentialtype= "None"/>
</security>
</binding>
</netMsmqBinding>
</bindings>

(3) Client:

Run the client, add the service reference, where the client's configuration file is to be modified, the security mode is modified and the host corresponds, set to none. The client test scenario is to call a service request every 2 seconds. We print the time here on the host side. After the client sends the message, the host is temporarily not started. Wait for some time. When you start the host, observe the message that the host prints. The code is as follows:

HTTP Netmsmqbinding_iwcfmsmqservice
Wcfmsmqserviceclient wcfserviceproxy = new Wcfmsmqserviceclient ("Netmsmqbinding_iwcfmsmqservice");
Call the SayHello service through the proxy, where the service invocation fails, and the message is sent to the queue for caching.
Console.WriteLine ("WCF First Call at:{0}", DateTime.Now);
WCFSERVICEPROXY.SAYHELLOMSMQ ("Frank");
Thread.Sleep (2000);//client sleeps for two seconds, continues the next call
Console.WriteLine ("WCF Second call At:{0}", DateTime.Now);
WCFSERVICEPROXY.SAYHELLOMSMQ ("Frank Xu");
Thread.Sleep (2000);//client sleeps for two seconds, continues the next call
Console.WriteLine ("WCF Last Call At:{0}", DateTime.Now);
WCFSERVICEPROXY.SAYHELLOMSMQ ("Frank Xu Lei");

(4) Operation result:

The first is the message sent to the client, we can then computer-management-service-dedicated queue to see,

The previous service required that we must start the host, or there would be a connection error. Here to test, stay for about 4 minutes after we launch the host. Observe the message that the host prints:

The host responds to the client's call operation after online.

Summary

This section of this article focuses on how WCF uses MSMQ to develop offline service operations. In addition to reviewing the basic concepts of MSMQ, the advantages and characteristics of the communication framework used by WCF services using MSMQ are described here. Finally, the implementation of offline service invocation based on WCF Message Queuing is given.

WCF's support and expansion of MSMQ Message Queuing greatly improves the scalability and flexibility of WCF service calls. Because of the relationship between development environment, this paper does not give the development process of public queue. In addition, in the WCF Message Queuing development process, everyone should pay attention to.

(1) Start the host on the service host, to make the queue existence judgment, in order to avoid the reading error.

(2) The queue service involves security issues, authentication and message encryption, signing, requiring certificate configuration. Condition restrictions There is no specific implementation process here.

(3) Another important concept of the WCF Queue Service is support for offline transactions. This feature is given in a later article.

(4) Operating environment: Xp Pro,.net 3.0 or more, vs2008, install the private Queue component service of at least MSMQ queue.

Finally, the reference code of this article is given:/files/frank_xl/wccfservicemsmqfrankxulei.rar.

WCF distributed development step for win: WCF service offline operation with Message Queuing MSMQ

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.