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

Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications: Over 85 easy recipes for managing communication between applications

eBook
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications

Chapter 2. Endpoint, Binding, and Behavior

In this chapter, we will cover:

  • Configuring Default Endpoints

  • Setting up two-way communication over MSMQ

  • Building a Publish-Subscribe service with dual binding

  • Creating a multiple-endpoint service

  • Implementing a POX HTTP service

  • Defining a CustomBinding without a timestamp header

  • Suppressing mustUnderstand validation on unknown SoapHeaders

  • Sharing a physical address between multiple endpoints

Introduction


WCF services are exposed through service endpoints, which provide the basic access point for client to utilize the functionality offered by a given WCF service. Service endpoints consist of ABC and a set of behaviors. What is ABC? Well, A stands for Address, which tells service consumers “Where is the service?”, B stands for Binding, which describes “How to talk to the service?” and C stands for Contract, which shows “What functionality can the service provide?”

WCF provides plenty of built-in bindings (such as BasicHttpBinding, NetTcpBinding, NetMsmqBinding, and so on), which can help developers host service endpoints over various transport protocols. Behaviors also play an important role in WCF. By using behaviors, we can gain further manipulation over the WCF service at service or endpoint level.

This chapter provides eight recipes on using the built-in binding and behaviors to build various service endpoints, which represent some of the useful scenarios in general WCF service...

Configuring Default Endpoints


When programming with WCF, we will often need to create some simple WCF services for testing our ServiceContracts. These services often use very simple and typical endpoint and binding definitions. However, every time we need to set up such a service, we have to define the same endpoint and binding settings again and again, which really adds much duplicated work. Fortunately, WCF 4.0 introduces the Default Endpoint feature which saves us from defining common endpoint/binding settings repeatedly.

How to do it...

The steps for using a default endpoint are quite straightforward:

  1. Create a new Console Application project in Visual Studio 2010 targeting .NET Framework 4.0.

  2. Add the ServiceContract in the service project and implementation types we need in the service project. We can use any valid ServiceContract and its corresponding implementation class here. For example, the following IHelloService service containing a single SayHello operation is used in our sample...

Setting up two-way communication over MSMQ


For a long time, MSMQ has been a great message-based communication component on the Microsoft platform. And the Microsoft .NET framework has also provided managed programming interfaces for developers to develop distributed applications based on MSMQ. However, it is still a bit complicated and time consuming for developers to build a complete distributed service through the raw or encapsulated MSMQ programming interface. As the new unified communication development platform on Windows, WCF provides more convenient means for developing a distributed service over the underlying MSMQ component.

Getting ready

If you are not yet familiar with Microsoft Message Queuing (MSMQ), you can get useful information on the following site:

http://msdn.microsoft.com/en-us/library/ms711472(VS.85).aspx

Also, the MSDN library has provided detailed reference on the .NET Framework System.Messaging namespace that encapsulates the raw MSMQ programming interfaces (visit http...

Building a Publish-Subscribe service with dual binding


Publish-Subscribe is a common design pattern that is widely used in client/server communication applications. In WCF service development, the Publish-Subscribe pattern will also help in those scenarios where the service application will expose data to certain groups of clients that are interested in the service and the data is provided to clients as a push model actively (instead of polling by the client). This recipe will demonstrate how to implement the Publish-Subscribe pattern in a WCF service through dual binding.

Getting ready

The Publish-Subscribe pattern is widely adopted in various application development scenarios and there are many different kinds of descriptions for this pattern. Refer to the following links for more information:

How to do it...

To implement the Publish-Subscribe pattern, we need to...

Creating a multiple-endpoint service


For traditional distributed communication services, we will open the service over a certain transport-specific endpoint such as HTTP endpoint. If we need to expose it via another different transport layer, we will probably have to add additional code to implement the new endpoint. The WCF programming model separates the service implementation and underlying transport layer so that we can conveniently expose a single service implementation via multiple heterogeneous endpoints (with different a transport layer and binding configuration).

How to do it...

  1. First, we make sure our ServiceContract is ready for various endpoint bindings we want to use to expose it. Some bindings may have special requirements on ServiceContract (such as MSMQ-based bindings). The following sample contract is ready for most built-in bindings:

      [ServiceContract]
        public interface ICounterService
        {
            [OperationContract]
            void Increment();
    
            [OperationContract...

Implementing a POX HTTP service


WCF services by default use SOAP as the message format of service operations, which means each message transferred between the client and service is wrapped in a SOAP envelope that contains one SOAP body and some SoapHeaders. However, sometimes our WCF service will need to work with some legacy POX (Plain Old XML) clients or our WCF-based client will need to talk to some POX-style service. In such cases, it is necessary to let our WCF client or service generate arbitrary XML messages without strictly obeying the SOAP standard.

How to do it...

We will use a CustomBinding to build a POX-enabled WCF service and consume it with a POX-enabled WCF client program. Let’s have a look at the complete steps:

  1. The first thing to do is to make our ServiceContract POX ready. The following code shows a sample operation that is ready for exchanging POX-style messages.

      [OperationContract(Action=”*”,ReplyAction=”*”)]
             Message SayHello(Message reqMsg);

    Compared with normal...

Defining a CustomBinding without a timestamp header


For WCF bindings that use message-layer security, a timestamp header will be added in the SOAP envelope to ensure the timely delivery of the message so as to prevent a potential message-replaying attach. However, some non-WCF service platforms may not expose this header. When working with this kind of service client or service, we will need to prevent the WCF message engine from generating the timestamp header.

How to do it...

Using WSHttpBinding as an example, we can create a customized binding that derives most of the setting of the built-in WSHttpBinding (but suppresses the timestamp header generation).

The following code snippet demonstrates how to create the CustomBinding and configure the certain binding element to disable timestamp header generation.

private static Binding GetCustomHttpBinding()
{
   WSHttpBinding wshttp = new WSHttpBinding();
   var bec = wshttp.CreateBindingElements();

   SecurityBindingElement secbe = bec.Find&lt...

Suppressing mustUnderstand validation on unknown SoapHeaders


For a WCF service or client proxy, it is common to receive SoapHeaders within the coming request or returned response messages. SoapHeader has a mustUnderstand attribute that indicates to the target endpoint (or intermediate message processor) whether the SoapHeader must be processed. The following screenshot shows a typical SOAP message that contains a SoapHeader with the mustUnderstand attribute set to 1 (true).

Also for a XML Web Service or WCF service, we can dynamically insert SoapHeaders at runtime or statically apply them at design-time (which will be described via WSDL or service metadata).

By default, a WCF endpoint will perform validation on all the SoapHeaders within incoming messages, and if there are any unknown SoapHeaders (not predefined) which have a mustUnderstand attribute as 1 (or true), a validation exception will be raised by the runtime. However, sometimes it is useful to suppress this validation so that the...

Sharing a physical address between multiple endpoints


WCF supports exposing a single service through multiple heterogeneous endpoints. Another great feature is letting multiple endpoints listen over the same physical transport address. For example, you want to host a WCF service that has multiple endpoints exposed. However, you only have a single HTTP URL open for listening. Then you can use this feature to make all those endpoints (as long as they’re using the same transport protocol) listen on the same URL.

How to do it...

Our sample service will expose two endpoints (one is IFoo and the other is IBar) and both of them will listen on the same HTTP URL:

  1. The first thing we need to do is configure the service endpoints. We will still specify different values for the address attribute of the two endpoints. However, we will use a special attribute named listenUri to supply the physical address (identical for both endpoints). The following screenshot shows the usage of both attributes:

  2. It is also...

Left arrow icon Right arrow icon

Key benefits

  • Master WCF concepts and implement them in real-world environments
  • An example-packed guide with clear explanations and screenshots to enable communication between applications and services and make robust SOA applications
  • Resolve frequently encountered issues effectively with simple and handy recipes
  • Explore the new features of the latest .NET 4.0 framework/Visual Studio 2010

Description

The Windows Communication Foundation 4.0 (WCF 4.0) is a .NET-based application programming interface for building and running connected systems. It enables secure and reliable communication among systems within an organization or across the Internet. This book deals with the difficult issues faced by a .NET developer while working with WCF.WCF 4.0 is a communications infrastructure that unifies a broad array of distributed systems' capabilities in a composable, extensible architecture that supports multiple transports, messaging patterns, encodings, network topologies, and hosting models. This book is a collection of focused real-world recipes and covers basic recipes on topics such as working with contracts to more advanced topics such as extending WCF runtime. By the end of this book you will have valuable information that helps transform the potentially unproductive habits of .Net developers who work with WCF.This book will take you through many concepts starting with complete support for contract-related design for WCF service development. You will learn to use WCF's built-in feature for building various service endpoints. Service hosting and configuration are important areas for building WCF services, especially at the service deployment stage, and are detailed in this book. You will find it easy to work with WCF client proxy generation and metadata publishing and discovery when you go through recipes such as customizing auto-generated service proxies.The author then discusses the exchange of data in WCF service operation features, related to WCF data serialization. You will discover some useful tips for security in WCF service development and built-in features for developing concurrency control for your services built upon it.One big plus is that you will learn to extend the existing WCF framework to achieve advanced functionality. You will find a dedicated chapter for RESTful and AJAX-enabled service development. Moving on, you will find several useful WCF service interoperability cases, which are important for a distributed service development platform. Towards the end of this book you will find some handy and useful diagnostic methods for troubleshooting.

Who is this book for?

If you work with Windows Communication Foundation 4.0 and you want to be efficient when working with WCF features such as interoperability, proxy generation, and security, you will find this book very useful. With this book you will be able to find quick and handy solutions for various kinds of service development scenarios using Microsoft Windows Communication Foundation 4.0. To follow the recipes you will need to be comfortable with .NET Framework, C# programming, and the basics of SOA and how to develop them.

What you will learn

  • Share a physical address between multiple endpoints
  • Custom binding without a Timestamp header
  • Bind a WPF element with data from WCF service
  • Invoke a WCF service without blocking the front UI
  • Build a WCF service with ad hoc security mode
  • Intercept WCF operation messages with MessageInspector
  • Extend WCF security with custom AuthorizationManager
  • Build REST-style services with WCF
  • Call WCF services from AJAX script clients

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 20, 2010
Length: 316 pages
Edition : 1st
Language : English
ISBN-13 : 9781849680776
Vendor :
Microsoft
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Oct 20, 2010
Length: 316 pages
Edition : 1st
Language : English
ISBN-13 : 9781849680776
Vendor :
Microsoft
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 132.97
WCF 4.0 Multi-tier Services Development with LINQ to Entities
€41.99
Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications
€41.99
Applied Architecture Patterns on the Microsoft Platform
€48.99
Total 132.97 Stars icon

Table of Contents

13 Chapters
Working with Contracts Chevron down icon Chevron up icon
Endpoint, Binding, and Behavior Chevron down icon Chevron up icon
Hosting and Configuration Chevron down icon Chevron up icon
Service Discovery and Proxy Generation Chevron down icon Chevron up icon
Channel and Messaging Chevron down icon Chevron up icon
Dealing with Data in Service Chevron down icon Chevron up icon
Security Chevron down icon Chevron up icon
Concurrency Chevron down icon Chevron up icon
Extending WCF Runtime Chevron down icon Chevron up icon
RESTful and AJAX-enabled WCF Services Chevron down icon Chevron up icon
Interoperability Chevron down icon Chevron up icon
Diagnostics Chevron down icon Chevron up icon
Miscellaneous WCF Development Tips Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8
(4 Ratings)
5 star 50%
4 star 25%
3 star 0%
2 star 0%
1 star 25%
Aykut Ucar Mar 22, 2011
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Short explanations and a quick sample after each explanation.For me, it is the perfect way of learning a concept.Examples are really detailed and make you understand what is going on behind.One of best tech books I read.
Amazon Verified review Amazon
C. JUNTAO Nov 10, 2010
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I've already had some WCF programming books. I think this one is an interesting and useful one as it is not focus on general WCF architecture or programming guideline, but provides many individual sample cases and tips about the questions or problems we might encounter. Also, I think it would be better if the book can add more advanced programming sample cases such as building a federation service or custom security token service.
Amazon Verified review Amazon
Assil Mar 31, 2013
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Even though it is less than 300 pages, It touches so many great advanced topics that you cant find in other books.It highlights many important topics, gives clear examples and guides you to the references for more information.I liked it.I gave 4 stars for all the good things I wish It was even more comprehensive to cover all the WCF in 1000 pages.
Amazon Verified review Amazon
R. Kumar Sep 28, 2012
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I am a senior .net developers for over 10 years now and I was looking for a book that can get me started with WCF however after start spending 7 days with it I can't even write a simple WCF service because I guess the book author expected everyone to be experienced WCF programmer. I believe a good book should always start from simple to advanced topics however here is totally opposite.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.