Published: 26 Jan 2011
By: Dhananjay Kumar

In this article we will see in detail three exciting features of WCF 4.0

Contents [hide]

Introduction

WCF 4 comes with many of new features for better developer experience. There is high integration with workflow services, and WCF REST services are more developer friendly. In short WCF 4 tries to address common scenarios easily and at the same time gives us more options to communicate in between service and client.

Below are a few of the new features of WCF 4.0:

  • Dynamic Service and End Point discovery
  • Intermediate Routing Pattern (Generic Routing Services)
  • Discovery announcement
  • Simplified Configuration
  • Protocol bridging and Fault tolerance
  • Default End Points.
  • .svc-less activation of REST services or making REST URI nice.
  • Default Protocol Mapping
  • Help page enabled for WCF REST service and many more.

In this article we will see in detail three exciting features of WCF 4.0. In subsequent articles we will examine the other features. In this article we are going to discuss

  • EndPoint Discovery
  • Default EndPoints
  • Help page enabled for WCF REST Service

Problem

People say that you need to know only the ABC of a service to expose End Point of that service. And client or service consumer needs to know only the End Point to consume the service. But wait, what if a binding needs to change at the service side from basic to WS over HTTP? This is a very basic change but to accommodate this the client has to update the service again. And this is very much error prone. It is a very tedious task to update the client about all the basic changes at the service side. To solve this issue, there is a mechanism called End Point Discovery or Dynamic Service.

In technical words, WCF 4.0 supports the WS-Discovery standard or protocol.

The WS-Discovery standard

WS-Discovery is a multicast protocol that issues SOAP messages over UDP.

WS-Discovery is a standard that defines a lightweight discovery mechanism for discovering services based on multicast messages. It enables a service to send a Hello announcement message when it is initialized and a Bye message when is removed from the network.

A client or consumer can discover services by multicasting a Probe message to which a service can reply with a ProbeMatch message containing the information necessary to contact the service.

A client or consumer can find services that have changed endpoint by issuing a Resolve message to which respond with a ResolveMatchmessage.

We can say that WS-Discovery is a UDP based multicast message exchange. This message receives End Point information from a Service and uses this as discovery information. A client uses discovery information to discover the available services on the network.

WCF Service Discovery API

WCF provides a WCF Discovery API for dynamic publish and discovery of web service using the WS –Discovery protocol. This API helps a service to publish them and helps a client to find services on the network.

Modes

Managed Mode

In managed mode there is a centralized server called a discovery proxy that services use to publish themselves and clients use to retrieve information about available services.

When a new service starts up it sends an announcement message to the discovery proxy.

The discovery proxy saves information about each available service to storage.

When a client must search for a service it sends a Probe request to the discovery proxy and it determines whether any of the services that have been published match the request.

If there are matches the discovery proxy sends a ProbeMatch response back to the client.

The client can then contact the service directly using the service information returned from the proxy.

Ad-Hoc Mode

There is no centralized server. Service announcements and client requests are sent in a multicast fashion.

If a service is configured to send out a Hello announcement on start up, it sends it out over a well-known, multicast address using the UDP protocol.

Therefore, clients have to actively listen for these announcements and process them accordingly.

When a client issues a Probe request for a service it is also sent over the network using a multicast protocol. Each service that receives the request determines whether it matches the criteria in the Probe request and responds directly to the client with a ProbeMatch message if the service matches the criteria specified in the Probe request.

Example

In the following example, we will create a very basic service and at the client side, the client will first discover the service in Ad-Hoc mode and then consume that.

To run and go through this sample, you need Visual Studio 2010 Beta.

To make a service discoverable, a ServiceDiscoveryBehavior must be added to the service host and a discovery endpoint must be added to specify where to listen for and send discovery message

Open VS2010. Create a new project by choosing WCF Service Application Project template. Delete all the code which is generated by Visual Studio. The IService implementation is as follows.

Listing 1: IService1.cs

Listing 2: Service1.cs

The above code demonstrates a simple Service Contract and its implementation.

Open the Web.Config file and modify the System.ServiceModel element. The result is shown in the following listing.

Listing 3: Web.Config

In the above code

  • One new EndPoint is being added.
  • The kind of newly added End Point is udpDiscoveryEndpoint.
  • This EndPoint will be used to discover the service.
  • A ServiceDiscovery behavior is also added.

Press F5 to run the service. The Service will get hosted in the built-in ASP.NET web server.

Now add a new console project to the solution. Add a reference to System.ServiceModel.Discovery.dll to the Console project.

Add the namespace System.ServiceModel.Discovery.

In the above code

  • We are instantiating an object of DiscoveryClient class. This class is used to discover available services.
  • In the constructor of this class, we are passing UdpDiscoveryEndPoint of the service.
  • Then we are calling the Find method with the contract name as argument to get FindResponse.
  • Now the FindResponse variable has all the available addresses for the contract passed in the Find method.
  • We are creating the client proxy and passing the discovered address as argument.
  • Finally, we're calling the service operation on the client proxy.

Figure 4: Output

Output

Default End Points and Protocol mapping

For a normal developer like me, it's been a great challenge working with WCF 3.x configuration of End Points in the config file. Developers had to add endpoints to setup a WCF service. In WCF 4, a Default End Point is associated with the service, if we don't configure any WCF endpoints.

Walkthrough

To see how the Default EndPoint works, follow the steps below.

First, create a WCF service application. Then, let us create two service contracts

Listing 4: IService1.cs

Listing 5: IService2.cs

Now let us implement the service as shown below.

Now we will host this service in a Console application, thus create a new Console application project.

Add a reference to a WCF service application project and also add a reference to System.ServiceModel in the Console application project.

Note

There is no App.Config associated with console application.

Here, we are registering two base addresses with the service host, one for http bindings and the other for nettcp bindings.

Now we don't have any configuration for the service endpoint, thus ServiceHost will create default endpoints for the two base addresses.

The default protocol mapping for default EndPoints are mapped as below.

Since HTTP is mapped with basicHttpBinding , we got the default EndPoint with basicHttpBinding.

The default EndPoint will only get created if there is not a single endpoint configured. If we add a single EndPoint then there won't be any default EndPoints configured.

If we add one EndPoint as below, we get the output shown in the next figure.

Now we see that if we configure an endpoint then WCF does not support default Endpoints.

If we closely look at the output, we see that by default WCF maps protocol to binding as below.

So if we are not configuring any endpoints, then the default endpoint created by WCF will be of kind basicHttpBinding, net.tcp type will be mapped to netTcpBinding and so on.

If we want to change this, we can change this default mapping:

With the above change, if we don't define any endpoints then WCF will create a default endpoint with a binding of wsHttpBinding. To demonstrate that, reate a WCF Service application and then create a Service contract as shown below.

Implement the Service as below.

Now we will host this service in a Console application, thus create a new Console application project. Add a reference to a WCF service application project and also add a reference to System.serviceModel in the Console application project.

Right click on the console project and add an app.config file to the Console project.

Now write the below code to fetch the default endpoint created by WCF.

And the output we get is shown in the next figure.

So we can see that we modified the default mapping using protocol mapping in System.ServiceModel

Enabling Help page for REST Services in WCF 4.0

WCF 4.0 provides a feature to eanable Help page on WCF REST Services.

Walkthrough

In this walkthrough we will see

  • How to create a REST based service
  • How to host a REST based service in a Console application
  • How to enable Help page for the REST Service

First, create a New project. Now select Console application as the project type.

Add a new project to the same solution. Choose the project type as class library.

Add the below references in both projects:

  • System.ServiceModel
  • System.ServiceModel.Description
  • System.ServiceModel.Web

If you are not able to get a reference to System.ServiceModel.Web dll by default, then follow the below steps:

  • Right click on your console application project or class library project
  • Select properties from the context menu
  • From the Application tab change the default .NET Framework 4 Client profile to .NET Framework 4.0.

Now you should able to see all the dll when you add a service reference to the project. Now, we are going to create the contract for the service.

Open the Contract (Class Library project), delete the Class1.cs file, then right click on Add a New Item and select Interface from the Code tab.

Mark the interface as public and add a ServiceContract attribute.

Declare two operation contracts. Add a WebGet attribute to the first one and a WebInvoke attribute to the other.

Listing 6: IService.cs

To implement the contract in the Service file, right click and add a class in Console application.

Give the class any name, such as Service.Then implement the interface IService.

Listing 7: Service.cs

Now we will host the service in a console application.Open Program.cs and create an instance of WebServieceHostFactory.

Add a service endpoint:

Add a service host behavior with help page enabled.

As we know, REST services use webHttpBindding, and we are enabling the help page here.

Just press F5 to run the service.

Once the service is up and running, you can thest the Help page by typing

http://localhost:8000/help

When you click on the Get or Post link you will get the help information shown below.

In the next article we will explore some other features of WCF 4.0.

<<  Previous Article Continue reading and see our next or previous articles Next Article >>

About Dhananjay Kumar

Dhananjay Kumar is a developer who blogs at http://debugmode.net/. He is Microsoft MVP ,Telerik MVP and Mindcracker MVP. You can follow him on twitter @debug_mode

This author has published 8 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


Walkthrough on WCF 4.0 Service in Silverlight 4.0
In this article we will see, how to create a WCF 4.0 Service.
Composing WCF applications
This article is taken from the book Dependency Injection in .NET. The author discusses WCF’s extensi...
Create your first RESTful service with WCF
Vishal Nayan explains how to create a RESTful service with WCF.
Calling WCF Duplex service In Silverlight
Vishal Nayan shows how to create a duplex WCF service at server side and let Silverlight access it a...
WCF as a Web Role and Worker Role in Windows Azure - Part 1
In this article we are going to see how WCF plays a major role in application development on Windows...

You might also be interested in the following related blog posts


Introducing SharePoint 2010 Training at U2U read more
Using WCF with SQL Azure and Telerik OpenAccess read more
The Underground at PDC read more
Visual Studio 2010 Extension Manager read more
Building A Product For Real read more
My History of Visual Studio (Part 6) read more
IIS Media Services 3.0 read more
F# in VS2010 read more
BeginDialOut with Office Communicator Clients read more
DotNetNuke Fusion Results for Q3 read more
Top
 
 
 

Discussion


Subject Author Date
placeholder Unviewable screen shots Ray Torres 2/4/2011 9:54 AM
RE: Unviewable screen shots Sonu Kapoor 2/4/2011 11:13 AM

Please login to rate or to leave a comment.

Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.