[WCF programming] 10. Operation: one-way operation, and 10 operation in wcf Programming

Source: Internet
Author: User

[WCF programming] 10. Operation: one-way operation, and 10 operation in wcf Programming
1. Overview of one-way Operations

WCF provides one-way operations. Once called by the client, WCF generates a request, but no relevant response information is returned to the client. Therefore, one-way operations do not return values, and any exceptions thrown by the service are not passed to the client.

Ideally, once the client calls a one-way operation, it will only be blocked at the moment of the request. In fact, one-way call is not equal to asynchronous call. When a one-way call arrives at the server, it does not distribute the call immediately. Instead, it distributes the call methods in the queue of the server at a certain time. This process depends on the concurrency mode of service configuration. The number of messages that the service needs to put into the queue is related to the channel and reliability mode. If the number of messages in the queue exceeds the capacity of the queue, the client will be blocked even if only one-way calls are sent. However, once the call is put into the queue, the blocking on the client will be canceled and the execution will continue. At the same time, the service will process this operation in the background.

Developers may mistakenly think that one-way operations are equivalent to concurrent calls. If the client uses the same proxy but uses multiple threads to call one-way operations, the calls on the server may be concurrent or not. In essence, this interaction is determined by the Service concurrency management mode and transmission session.

All WCF bindings support one-way operations.

2. Configure unidirectional operations

The OperationContract feature defines the IsOneWay attribute of the Boolean Type:

public sealed class OperationContractAttribute : Attribute{   public bool IsOneWay { get; set; }}

The default value of the IsOneWay attribute is false, that is, the default operation is a request/response operation. If the IsOneWay attribute is set to true, the method becomes a one-way operation.

[ServiceContract] public interface IService7 {    [OperationContract(IsOneWay=true)]    void MyMethod(); }

When one-way operations are called, the client does not have any special beginning. The value of the IsOneWay attribute is included in the Service metadata. Note that the IsOneWay value in the service contract definition is the same as that in the client import definition.

Because a one-way operation does not respond to a message, it cannot contain the return value. As follows:

[ServiceContract] public interface IService7 {    [OperationContract(IsOneWay=true)]    void MyMethod(); }

In fact, when loading the host or opening the proxy, WCF will force the signature of the authentication method.

Iii. Unidirectional operation and Reliability

The client does not care about the call result, and does not mean that it does not care about whether the call is made. All in all, even if one-way operations are adopted, the service reliability must be ensured so that requests can be correctly transmitted to the service.

However, the client does not consider the calling sequence of one-way operations, which is the main reason why WCF allows developers to separate effective reliability transmission from effective ordered transmission and message execution.

Iv. Unidirectional operations and Session Services

WCF allows developers to design a session contract with one-way operations:

[ServiceContract(SessionMode=SessionMode.Required)]public interface IService7{    [OperationContract(IsOneWay=true)]    void MyMethod();}

In this configuration, if the client sends a one-way operation, the proxy is closed when the method is executed, and the client is blocked until the operation is completed.

Although technically feasible, it is a bad design to include a one-way operation in a session contract. Because having a session often means that the service needs to be managed to represent the status of the client. Any exception may damage this status, and the client cannot obtain an exception. In addition, the client or service selects a session interaction because the contract it uses needs to complete the lock step execution through a state machine. Monotonous operations cannot meet the requirements of this mode. Therefore, we recommend that you only apply one-way operations to one-way services or Singleton services.

If one-way operations are defined in the session contract, you must ensure that one-way operations are the last operation to terminate the session. This can be achieved through the distribution operation:

[ServiceContract(SessionMode=SessionMode.Required)]public interface IService7{    [OperationContract()]    void MyMethod();    [OperationContract(IsOneWay = true,IsInitiating=false,IsTerminating=true)]    void Over();}
5. Unidirectional operations and exceptions

Even if the one-way operation does not return a value, it does not return an exception from the server. However, the client still needs to obtain an exception of one-way call. It is inferred that the call has failed in the service segment.

If there is no transmission SESSION (bound using BasicHttpBingding or without reliable message transmission and secure WsHttpBingding binding), the client will not be affected if an exception occurs during a one-way operation, will continue to send calls to the same proxy instance:

[ServiceContract] public interface IService7 {[OperationContract (IsOneWay = true)] void MethodWithError (); [OperationContract] void commit ();} [ServiceBehavior (InstanceContextMode = response. perCall, ConcurrencyMode = ConcurrencyMode. single)] public class Service7: IService7 {public void MethodWithError () {throw new Exception ();} public void MethodWithoutError () {}}// does not have a transfer session MyContractClient proxy = new MyContractClient (); proxy. methodWithError (); proxy. methodWithoutError (); proxy. close ();

If a transfer session exists, an exception on the server (including an exception thrown by one-way Operations) will cause an operation channel error. At this time, the client cannot make any new call using the same proxy instance.

[ServiceContract] public interface IService7 {[OperationContract (IsOneWay = true)] void MethodWithError (); [OperationContract] void commit ();} [ServiceBehavior (InstanceContextMode = response. perCall, ConcurrencyMode = ConcurrencyMode. single)] public class Service7: IService7 {public void MethodWithError () {throw new Exception ();} public void MethodWithoutError () {}}// has the transport session MyContractClient proxy = new MyContractClient (); proxy. methodWithError (); try {proxy. methodWithoutError (); // a proxy is thrown because of a channel error. close ();} catch {}

The client cannot even close the proxy safely. Therefore, one-way operations do not have the instant release feature, because the client will find errors on the server during the call.

In the future, we will show you how to use one-way operations for truly asynchronous instant release and discard operations.


The usage of WCF programming is high in actual system development.

Most of the general management systems use the B/S architecture. The mature C/S architecture still uses many WebServices, but WCF is much more advanced than WebService, unless you do not fully understand WCF, otherwise, we recommend that you use WCF. Of course, you can also use WCF in the B/S architecture. For example, you can use the ASP. NET AJAX framework to call the WCF Service (traditionally WebService ).

WCF Service programming vs comprehensive analysis of WCF

I think you can first look at the comprehensive analysis of WCF, because I am also reading it. I think the analysis in this book is in place and is suitable for beginners.

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.