Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications

You're reading from   Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications Over 85 easy recipes for managing communication between applications

Arrow left icon
Product type Paperback
Published in Oct 2010
Publisher Packt
ISBN-13 9781849680769
Length 316 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Juntao Cheng Juntao Cheng
Author Profile Icon Juntao Cheng
Juntao Cheng
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications
Credits
Foreword
About the Author
About the Reviewers
Preface
1. Working with Contracts 2. Endpoint, Binding, and Behavior FREE CHAPTER 3. Hosting and Configuration 4. Service Discovery and Proxy Generation 5. Channel and Messaging 6. Dealing with Data in Service 7. Security 8. Concurrency 9. Extending WCF Runtime 10. RESTful and AJAX-enabled WCF Services 11. Interoperability 12. Diagnostics 13. Miscellaneous WCF Development Tips Index

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...

  1. Create the service interface or class type and add methods into it.

  2. Mark the interface/class type with ServiceContractAttribute and mark the methods with OperationContractAttribute.

  3. Set the IsOneWay property of the OperationContractAttribute to true.

    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

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime