Use WCF Based on MSMQ)

Source: Internet
Author: User
Tags msmq

On Windows, MSMQ is the preferred message delivery middleware. It is a high-speed, asynchronous, and reliable communication mechanism. When two applications on the internet need to exchange information, this middleware may be required.
WCF is fully oriented to SOA and greatly simplifies various distributed solutions with different styles in the past. A recent project needs to use the SOA architecture, while the underlying layer needs to use MSMQ as the messaging infrastructure. So I have studied the MSMQ method in WCF over the past two days. The following is an example.
First, define the contract on which the server and client communicate. These contacts are usually defined in a separate DLL, so that they can be referenced by the server and client. Let's assume a simple contract, that is, an interface icalculate:

[Servicecontract]
Public   Interface Icalculate
{
[Operationcontract (isoneway = True )]
Void Dealorder ( String Orderid );
}

In this example, we define icalculate in wcflib. dll.

The server must implement the icalculate interface: Public   Class Calculator: icalculate
{
Public   Void Dealorder ( String Orderid)
{
Program. filelogger. Log (orderid );
}
}

Next, the server can publish the service in the form of MSMQ, which can be configured in the configuration file app. config:<? XML version = " 1.0 " Encoding = " UTF-8 "   ?>
< Configuration >
< System. servicemodel >
< Services >
< Service name = " Wcftest.Calculator " >
< EndpointAddress = " Net. MSMQ: // localhost/private/Wcftest "
Binding = " Netmsmqbinding "  Bindingconfiguration = " MSMQ "
Contract = " Wcflib. icalculate " />  
</ Service >
</ Services >

 

 

< Bindings >
< Netmsmqbinding >
< Binding name = " MSMQ " >
<Security Mode="None"/>
</ Binding >
</ Netmsmqbinding >
</ Bindings >

</ System. servicemodel >
</ Configuration >

The red part in the configuration marks the "ABC" of WCF ",AddressIndicates that the local dedicated queue named wcftest will be used. Please note that,BindingThere isBindingconfiguration, Indicating that the binding requires more advanced configuration. The corresponding configuration section is inBindingsIn segment, because the message queue used in the example does not use the domain modeSecurity ModeSet to none. This configuration sets the msmqauthenticationmode attribute to msmqauthenticationmode. None. In addition, the wcftest dedicated queue displayed in the configuration needs to be set to "transactional". You can select this attribute when creating the queue.
After the configuration is complete, we can start the service:Servicehost= NewServicehost (Typeof(Calculator ));
Servicehost. open ();


Let's look at the client. It's very simple. First, set "ABC" in APP. config (consistent with the server ): <? XML version = " 1.0 " Encoding = " UTF-8 "   ?>
< Configuration >
< System. servicemodel >
< Client >
< Endpoint name = " Calculatorclient "  
Address = " Net. MSMQ: // localhost/private/wcftest "
Binding = " Netmsmqbinding "  Bindingconfiguration = " MSMQ "
Contract = " Wcflib. icalculate " >
</ Endpoint >
</ Client >

< Bindings >
< Netmsmqbinding >
< Binding name = " MSMQ " >
<Security Mode="None"/>
</ Binding >
</ Netmsmqbinding >
</ Bindings >
</ System. servicemodel >
</ Configuration >

After wcflib. dll is referenced, you can call the service: Channelfactory < Wcflib. icalculate > Channelfactory =   New Channelfactory < Icalculate > ( " Calculatorclient " );
Icalculate calculate = Channelfactory. createchannel ();
  Calculate. dealorder ( This . Textbox1.text );

After MSMQ is used as the message transmission infrastructure, the client can still call the dealorder method to send messages when the Internet is unavailable or the server is not started. Of course, messages are stored in the queue. After the network is restored or the server is started, messages in these queues are processed.

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.