WCF Service-Oriented Application series 17: message exchange mode (MEP)-One-way operation

Source: Internet
Author: User

The one-way operation does not return values, and the client does not care whether the call is successful or not. It is an instant call method that marks the operation as one-way by using operationcontractattribute, only <input> messages are included in the WSDL description. Any exceptions thrown by the server are not transmitted to the client. Ideally, once the client calls a one-way method, it will only be blocked at the moment of calling distribution. In fact, one-way invocation is not equivalent to asynchronous invocation. When one-way calls arrive at the server, they are not immediately distributed, but may be placed in the queue of the server and distributed at a certain time. This process is related to the concurrent mode behavior configured by the Service, the maximum number of messages in the queue, the configured channel, and the reliability. If the number of queue messages exceeds the queue capacity, the client should be blocked. However, once the call is put into the queue, blocking on the client will be canceled. All WCF bindings support one-way operations.

Although any exceptions thrown by the server are not transmitted to the client, communication problems (such as address errors or unavailability of the host) occur when one-way operations are distributed) an error occurs during the delivery call. An exception is thrown on the client when an operation is attempted.

In a monotonous service, if there is no transfer session, when a one-way operation is called, if an exception occurs, the client will not be affected, and the same proxy instance will continue to be called. However, if you use a secure wshttpbinding binding, or use nettcpbinding and netnamedpipebinding that do not contain reliable message transmission, a server exception, including one-way operation throws an exception, A channel error occurs. At this time, the client cannot send any new call using the same proxy instance, and the client cannot even safely close the proxy. When the wshttpbinding binding or nettcpbinding of reliable message transmission is used, exceptions do not cause channel errors. In this case, the client can continue to issue calls.

One-way operation scenarios: fire-and-forget, do not need to report successful operations, such as log activity or event publishing; polling, the client uses a single request to obtain the results of long-running operations. duplex and one-way operations are particularly suitable for bidirectional communication when callback operations are involved.

Next, we will introduce one-way operations through a demo:

MainCodeAs follows:

Contract interface code:

  [Servicecontract (namespace  =     "  Http://schemas.xinhaijulan.com/demos/OneWay  "  )]
Public Interface Imycontract
{
[Operationcontract (isoneway = True )]
Void Methodwitherror ();

[Operationcontract]
Void Methodwithouterror ();
}

 

 

Implementation Code:

    Public     Class  Mycontract: imycontract
{
Public Void Methodwitherror ()
{
Console. writeline ( " Execute methodwitherror " );
Throw New Exception ();
}

Public Void Methodwithouterror ()
{
Console. writeline ( " Execute methodwithouterror " );
}
}

 

 

Server code:

    Static     Void  Main (  String  [] ARGs)
{
Startbasichttpbindingserver ();
// Startnettcpbindingserver ();
}

Private Static Void Startbasichttpbindingserver ()
{
Using (Servicehost host = New Servicehost ( Typeof (Servicelibrary. mycontract )))
{
Host. addserviceendpoint ( Typeof (Servicelibrary. imycontract ), New Basichttpbinding (), " HTTP: /localhost: 8001/mycontract " );
Host. open ();
Console. writeline ( " Please input exit to close host. " );
String Key = Console. Readline ();
While (Key. tolower () ! = " Exit " )
{
Console. writeline ( " Please input exit to close host. " );
Key = Console. Readline ();
}
}
}

Private Static Void Startnettcpbindingserver ()
{
Using (Servicehost host = New Servicehost ( Typeof (Servicelibrary. mycontract )))
{
Host. addserviceendpoint ( Typeof (Servicelibrary. imycontract ), New Nettcpbinding (), " Net. TCP: // localhost: 9001/mycontract " );
Host. open ();
Console. writeline ( " Please input exit to close host. " );
String Key = Console. Readline ();
While (Key. tolower () ! = " Exit " )
{
Console. writeline ( " Please input exit to close host. " );
Key = Console. Readline ();
}
}
}

 

 

Client code:

    Static     Void  Main (  String  [] ARGs)
{
Testbasichttpbinding ();
// Testnettcpbinding ();

Console. Readline ();
}

Private Static Void Testbasichttpbinding ()
{
Channelfactory < Servicelibrary. imycontract > Channelfactory = New Channelfactory < Servicelibrary. imycontract > ( New Basichttpbinding ());
Servicelibrary. imycontract Client = Channelfactory. createchannel ( New Endpointaddress ( " HTTP: /localhost: 8001/mycontract " ));

Using (Client As Idisposable)
{
Console. writeline ( " ------------ Testbasichttpbinding begin ------------- " );
Client. methodwitherror ();
Client. methodwithouterror ();
Console. writeline ( " ------------ Testbasichttpbinding end --------------- " );
}
}

Private Static Void Testnettcpbinding ()
{
Channelfactory < Servicelibrary. imycontract > Channelfactory = New Channelfactory < Servicelibrary. imycontract > ( New Nettcpbinding ());
Servicelibrary. imycontract Client = Channelfactory. createchannel ( New Endpointaddress ( " Net. TCP: // localhost: 9001/mycontract " ));

Using (Client As Idisposable)
{
Console. writeline ( " ------------ Testnettcpbinding begin ------------- " );
Client. methodwitherror ();
Client. methodwithouterror ();
Console. writeline ( " ------------ Testnettcpbinding end --------------- " );
}
}

 

A: Test basichttpbinding.

The homepage is enabled on the server: startbasichttpbindingserver () method, comment on the startnettcpbindingserver () method, enable the testbasichttpbinding () method on the client, comment on the testnettcpbinding () method, and then start the server, the output is as follows:

Server output:

  Please input exit to close host.
Execute methodwithouterror
Execute methodwitherror

Client output:

 
  ------------Testbasichttpbinding begin-------------
------------Testbasichttpbinding end---------------

This indicates that when no session is transmitted, the server exception is not passed to the client. After the server throws an exception, the client can still run normally.

B: Test nettcpbinding. 

The homepage is enabled on the server: startnettcpbindingserver () method, comment on startbasichttpbindingserver () method, enable the testnettcpbinding () method on the client, comment on the testbasichttpbinding () method, and then start the server, the output is as follows:

Server output:

 
  Please input exit to close host.
Execute methodwitherror

Client output:

 
  ------------Testnettcpbinding begin-------------

At the same time, when the client uses the same proxy for new calls, an error is thrown as follows:

This indicates that if nettcpbinding and netnamedpipebinding are used without reliable message transmission, a server exception, including one-way operation throws an exception, will cause a channel error.

So far, one-way operations have been introduced.

Click to download the demo.

 
  Author: Xinhai julan
Source:Http://xinhaijulan.cnblogs.com
The copyright is shared by the author and the blog. You are welcome to reprint the copyright. However, you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.

 

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.