Defining a one-way Contract
One-way (also called diagram-style) operation is a common pattern in distributed communication programming and is also one of the three supported message exchange patterns in WCF. When using the one-way message exchange pattern, a client sends a message using a fire-and-forget exchange (refer to the next screenshot). A fire-and-forget exchange is one that requires out-of-band confirmation of successful delivery. The message might be lost in transit and never reach the service. If the send operation completes successfully at the client end, it does not guarantee that the remote endpoint has received the message. In those cases where the client only wants to send information to the server side, without taking care of the execution result, we can consider defining our WCF service operation as one-way style.
How to do it...
Create the service interface or class type and add methods into it.
Mark the interface/class type with
ServiceContractAttribute
and mark the methods withOperationContractAttribute
.Set the
IsOneWay
property of theOperationContractAttribute
totrue
.The following code snippet shows the complete one-way
OperationContract
definition:[ServiceContract] interface IMyContract { [OperationContract(IsOneWay=true)]void OneWayMethod(){ // Do some work here } }
How it works...
When OperationContract
is marked with IsOneWay=true
, the runtime will detect this and know that this service operation needs to be handled as one-way style. One-way operation cannot carry a return value but can only pass input parameters to the service. After the client sends out the service request, the client will wait until it gets the response that the request message has reached the service side. However, the response here is not the return value, but the protocol level ACK, which indicates that the request has reached the service (but gives no idea of whether or how the request has been processed).
We can get further understanding on one-way operation via the following question:
What is the difference between a standard void (no return value) operation and a one-way operation?
Suppose you have the following ServiceContract
implemented:
[ServiceContract] public interface IHelloService { [OperationContract(IsOneWay=false)] void DoWork(); [OperationContract(IsOneWay = true)] void DoWorkAsOneWay(); }
By invoking the two operations from the client and capturing the HTTP message, we can get different response messages as shown in the next two screenshots. The first screenshot shows the response of the DoWork
operation, while the next shows the response of the DoWorkAsOneWay
operation.
As you can see, the normal void operation will return HTTP 200 status code and the complete SOAP Response in the body, while the one-way operation will only return a HTTP 202 Accepted status header. This indicates that the one-way operation call gets finished as long as the server side received the request, while the normal void operation (standard request/reply) will wait for the server side to execute and return the response data. Understanding this can help us to make better decisions about whether to use one-way operation or not.
There's more...
In addition to one-way operation, there are two other message exchange patterns that are widely used in WCF services. They are the Request-response pattern and the Duplex pattern.
The Request-response pattern is very similar to the standard function call that has an input parameter and return value. In a Request-response pattern-based WCF service operation call, a message is sent and a reply is received. The pattern consists of request-response pairs, as shown in the next figure.
The Duplex exchange pattern allows an arbitrary number of messages to be sent by a client and received in any order. This pattern is like a phone conversation, where each word being spoken is a message (refer to the following screenshot).
See also
Capture a raw http request/response of WCF service call in Chapter 12
Complete source code for this recipe can be found in the
\Chapter 1\recipe1\
folder