New Features in WCF 4.0-Part 1

Pratikshagzb
Posted by in WCF category on for Beginner level | Points: 150 | Views : 12736 red flag
Rating: 3 out of 5  
 1 vote(s)

This article describes the new Features of WCF 4.0


 Download source code for New Features in WCF 4.0-Part 1

 

 

This is the Part 1 of this article, shortly I shall write Part 2

I will be covering in this article 

  1. Dynamic WCF  Service and End Point discovery. 
  2. Intermediate Routing Pattern. 
  3. Discovery announcement.

1. Dynamic WCF  Service and End Point discovery


It allows us to Discover what Services are running and their location. It sends Hello announcement on initialization and Bye when removed from Network. Discovery API provided by WCF helps Client to find service on the network by using Discovey Standard or protocol. Client can send multicast Probe message to discover service to which Service can reply with Probe match message which contains information to  connect to service.For Services that have changed Endpoint Resolve message can be sent to which Service can respond with ResolveMatch message.

Discovery API Modes-

1. Managed Mode - Discovery Proxy is the Central Server which Services use to publish themselves and Client use to get information. This is done by Announcement message to Discovery Proxy so that all Clients  can know all the Services that are up and running. Client sends Probe message to which  Server responds with Probe match

2. Ad-Hoc Mode - No Central Server. All announcement and request are done via Multicast. Clients have to listen for these announcements. Client even sends Probe message via multicast.


Following is the config file for  WCF Service-

<system.serviceModel>

    <services>

      <service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">

        <!-- Service Endpoints -->

        <endpoint address="" binding="wsHttpBinding" contract="WcfService1.IService1">

          <!--

  Upon deployment, the following identity element should be removed or replaced to reflect the

  identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity

  automatically.

  -->

          <identity>

            <dns value="localhost"/>

          </identity>

        </endpoint>

        <endpoint name ="udpDiscovery" kind ="udpDiscoveryEndpoint" />

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

      </service>

        </services>

 

    <behaviors>

      <serviceBehaviors>

        <behavior name="WcfService1.Service1Behavior">

          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->

          <serviceMetadata httpGetEnabled="true"/>

          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->

          <serviceDebug includeExceptionDetailInFaults="false"/>

          <serviceDiscovery />

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

 

With this configuration we enable service and Endpoint discovery for our service. Here we use a new feature of WCF4 called standard endpoints (by using  the "kind" attribute on the udpDiscovery endpoint).


If Client wants to talk to a service which supports endpoints for IService1 we  can use  client-side discovery API like this:

DiscoveryClient discoverclient = new DiscoveryClient(new UdpDiscoveryEndpoint()); 

 

  FindResponse response = discoverclient.Find(new FindCriteria(typeof(IService1))); 

 

 EndpointAddress address = response.Endpoints[0].Address; 

 

  Service1Client client = new Service1Client(new  WSHttpBinding(), address); 

 

  string str= client.CheckMessage("Hello Testing Dynamic Service and EndPoint Discovery in  WCF 4 ."); 

 

  Console.WriteLine(str); 

 

But what if the Binding changes?

For this We can pass a MEX (MetadataExchange, based on WS-MetadataExchange) address to the static Resolve method.

 

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>


2. Intermediate Routing Pattern

We have  one service entry point with a universal contract and we  route incoming messages to the corresponding  business services.


We use System.ServiceModel.Routing.RoutingService..Generic routing service in WCF4  enables routing for different message exchange patterns .We implement routing service by using Message Filters.

Hosting Routing Service

ServiceHost serviceHost = new ServiceHost(typeof(RoutingService));

            try

            {

                serviceHost.Open();

                Console.ReadLine();

                serviceHost.Close();

            }

            catch (CommunicationException)

            {

                serviceHost.Abort();

            }

 

Routing Service by Message Filters

<configuration>

<system.serviceModel>

<services>

<service behaviorConfiguration="routingData" name="System.ServiceModel.Routing.RoutingService">

<host>

<baseAddresses>

<add baseAddress="http://localhost/Services/WCFSample"/>

</baseAddresses>

</host>

<endpoint address="" binding="basicHttpBinding" name="requestReplyEndpoint" contract="System.ServiceModel.Routing.IRequestReplyRouter"/>

</service>

</services>

<behaviors>

<serviceBehaviors>

<behavior name="routingData">

<serviceMetadata httpGetEnabled="True"/>

<routing filterTableName="mainRoutingTable"/>

</behavior>

</serviceBehaviors>

</behaviors>

<client>

<endpoint name="TestService" address="http://localhost:8080/Services/TestWCFSample" binding="basicHttpBinding" contract="*"/>

</client>

<routing>

<filters>

<filter name="MatchAllFilter" filterType="MatchAll"/>

</filters>

<filterTables >

<filterTable name="mainRoutingTable">

<add filterName="MatchAllFilter" endpointName="IntermediateRoutingSample"/>

</filterTable>

</filterTables>

</routing>

</system.serviceModel>

<system.web>

<compilation debug="true"/></system.web></configuration>

 

Here we are exposing  IRequestReplyRouter at  http://localhost:8080/Services/WCFSample to the consumers. Based on the message filter in the entry in the routing table, the messages received by the service will be sent to the endpoint .

Client side-

<configuration>

<system.serviceModel>

<client>

<endpoint address="http://localhost:8080/Services/WCFSample"

binding="basicHttpBinding"

contract="IHelloService" />

</client>

</system.serviceModel>

</configuration>

3. Discovery Announcement


 
 This shows when my Service is Online or Offline. This is done by Announcement Endpoint. This allows Clients who are listening to learn about new services. This reduces the amount of Probing/Multicast messaging.

    

We can configure a service with an announcement endpoint by using the <serviceDiscovery> behavior. The <serviceDiscovery> behavior allows us to define a collection of announcement endpoints that will be exposed by the service. We can use the standard “udpAnnouncementEndpoint” .

 

We  also  need to configure the service with a standard “udpDiscoveryEndpoint” if we want it to respond to discovery probes initiated by clients. The following example shows a typical configuration:

<configuration>

<system.serviceModel>

<services>

<service name="WcfService1"

behaviorConfiguration="serviceBehavior">

<endpoint address=""

binding="basicHttpBinding"

contract="IService1" />

<endpoint name="udpDiscovery" kind="udpDiscoveryEndpoint"/>

</service>

</services>

<behaviors>

<serviceBehaviors>

<behavior name="serviceBehavior">

<serviceDiscovery>

<announcementEndpoints>

<endpoint name="udpAnnouncement" kind="udpAnnouncementEndpoint"/>

</announcementEndpoints>

</serviceDiscovery>

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

</configuration

With this configuration , the service will announce  when it comes online and it will also announce when it’s going offline. W’ll have to specifically design  clients to listen for them at runtime. We can  do this by hosting an announcement service in client application that uses  WS-Discovery announcement protocol.

WCF 4 comes with a class called AnnouncementService designed  for this purpose. The AnnouncementService provides two event handlers:

1. OnlineAnnouncementReceived 

2. OfflineAnnouncementReceived.

 Client applications can simply host an instance of the AnnouncementService using ServiceHost and register event handlers for these two events.

Whenever a service comes online and announces itself, the client-hosted AnnouncementService will receive the “online” announcement and OnlineAnnouncementReceived will fire in the client. When the service goes offline, it will send an “offline” announcement and OfflineAnnouncementReceived will fire in the client.

That's all for Part 1.  I will be covering the remaining part in next  Article. So stay tuned.

I am open for discussions and valuable feedback.

Page copy protected against web site content infringement by Copyscape

About the Author

Pratikshagzb
Full Name: Pratiksha Saxena
Member Level: Starter
Member Status: Member
Member Since: 3/26/2010 12:52:14 AM
Country: India



Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)