New Features in WCF 4.0-Part 2

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

This is Part 2 and concluding part of my Article New Features of WCF 4.0.


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

Here I will cover

        •  Simplified Configuration
        •  Protocol bridging and Fault tolerance
        •  Standard End Points.
        •  .svc-less activation of REST services or making REST URI nice.


You can refer to Part 1 of the Article from the below link-

Part-1 http://www.dotnetfunda.com/articles/article1013-new-features-in-wcf-40part-1-.aspx

1. Simplified Configuration





In WCF 3.5 when we host  Service without  Endpoint we get  exception saying that we need to configure at least a single Endpoint.   But in WCF 4.0 this is not the case because Service Runtime will automatically adds default endpoints for us.

Lets see how this works –

Code for IService1-

[ServiceContract]

    public interface IService1

    {

        [OperationContract]

        string GetData(int value);

    }



Code for Service1 -

public string GetData(int value)

        {

            return string.Format("You entered: {0}", value);

        }



 

Now add ConsoleApplication1 in the Solution.

Add following in App.config.

<system.serviceModel>

    <behaviors>

      <serviceBehaviors>

        <behavior>

          <serviceMetadata httpGetEnabled ="true"/>

        </behavior>

      </serviceBehaviors>

    </behaviors>

    <protocolMapping>

      <add binding="wsHttpBinding" scheme ="http"/>

    </protocolMapping>

  </system.serviceModel>

Add the following code in Program.cs-

static void Main(string[] args)

        {

            ServiceHost Host = new ServiceHost(typeof(WcfSimpleService1.Service1), new Uri("http://localhost:7777/Services/Hello"));

            Host.Open();

            Console.WriteLine("Wow Started....");

            Console.ReadLine();

            Host.Close();

            Console.ReadLine();

        }

 



Put breakpoint on Host.Open and run ConsoleApplication.Since protocol mapping is set to wsHttpBinding it uses wsHttpBinding as shown in fig-

 

You can also download the attached project file.

2. Protocol bridging and Fault tolerance

Protocol bridging is basically used for transport. As in fig.  Client sends message to Routing Service via HTTP .Routing Service will send the message to Backend WCF Service via TCP/IP.Here we are using Protocol bridging between HTTP AND TCP/IP.

Fault tolerance is alternate endpoints (any no of endpoints) which will  be called when Exception occurs in the RoutingService while calling the original target endpoint.

We are  exposing  our RoutingService via an basicHttpBinding-based request-reply endpoint.

      <service behaviorConfiguration="routingData"

    name="System.ServiceModel.Routing.RoutingService">

        <host>

          <baseAddresses>

            <add  baseAddress="http://localhost:3562/Services/Hello"/>

          </baseAddresses>

        </host>

 

        <endpoint address=""

                  binding="basicHttpBinding"

                  name="requestReplyEndpoint"

                  contract="System.ServiceModel.Routing.IRequestReplyRouter" />

      </service>

 

 

We have actually two services TestService1 and TestService2.One is exposed via basicHttpBinding and other through netTcpBinding.

We have configured our router in such a way that it still accepts incoming messages via basicHttpBinding  but on need arising it  can route messages to the netTcpBinding-based service.

  <client>

        <endpoint name="TestService1"

                  address="net.tcp://localhost:3562/Services/Test"

                  binding="netTcpBinding"

                  contract="*" />

        <endpoint name="TestService2"

                  address="http://localhost:3562/Services/Test"

                  binding="basicHttpBinding"

                  contract="*" />

  </client>

Routing table to route to netTcpBinding-based service.

<routingTables>

        <table name="mainRoutingTable">

          <entries>

            <add filterName="xPath1" endpointName="TestService1" />

          </entries>

        </table>

</routingTables>

 

We can specify alternate endpoints (any no of endpoints) which will  be called when Exception occurs in the RoutingService while calling the original target endpoint.

<routingTables>

        <table name="mainRoutingTable">

          <entries>

            <add filterName="xPath1" endpointName="TestService1"                         alternateEndpoints="endpointsList" />

          </entries>

        </table>

  </routingTables>

  <alternateEndpoints>

        <list name="endpointsList">

          <endpoints>

            <add endpointName="TestService2" />

            </endpoints>

        </list>

  </alternateEndpoints>

 

Here we have TestService2 endpoint which is exposed via  basicHttpBinding endpoint.

3. Standard End Points


Standard endpoints(For this use kind attribute ) enable a developer to define an endpoint that has default values . These endpoints allow  to use  an endpoint without  specifying information of a static nature. Standard endpoints can be used for infrastructure and application endpoints. Standard endpoints  exposes your service for metadata exchange. Same result can be accomplished by using the “mexEndpoint” standard endpoint as follows:

 

<system.serviceModel>

    <services>

      <service name="HelloService">

        <endpoint kind="udpDiscoveryEndpoint" />

      </service>

    </services>

    <behaviors>

      <behavior>

        <serviceDiscovery />

      </behavior>

    </behaviors>

  </system.serviceModel>

 

 

 

We can also  specify a reusable endpointConfiguration for the endpoint using the new standardEndpoints section in Web.config

 

 

<system.serviceModel>

    <services>

      <service name="TestService">

        <endpoint

            kind="udpDiscoveryEndpoint"

            endpointConfiguration="udpDiscoveryEndpointSettings"/>

      </service>

    </services>

    <standardEndpoints>

      <udpDiscoveryEndpoint>

        <standardEndpoint

          name="udpDiscoveryEndpointSettings"

          multicastAddress="soap.udp://239.255.255.252:5762"

          maxResponseDelay="00:00:07">

          <transportSettings

              duplicateMessageHistoryLength="2048"

              maxPendingMessageCount="5"

              maxReceivedMessageSize="8192"

              maxBufferPoolSize="262144"/>

        </standardEndpoint>

      </udpDiscoveryEndpoint>

    </standardEndpoints>

    <behaviors>

      <behavior>

        <serviceDiscovery />

      </behavior>

    </behaviors>

  </system.serviceModel>

4. .svc-less activation of REST services.

It is specially needed in  WAS-hosting WCF services.If we have to write  http://xyz/ CalulatorService.svc .svc at the end of url makes it user unfriendly. It also makes it Low REST service as it does not follows REST URI principle.

Till date developers have to overcome this limitation by implementing URL ReWrite module in IIS. WCF 4.0 has introduced a feature to access WCF services using attribute called as relativeAddress.

<system.serviceModel>

    <serviceHostingEnvironment>

      <serviceActivations>

        <add relativeAddress="/Calculator" service="CalulatorService.svc"/>

      </serviceActivations>

    </serviceHostingEnvironment>

  </system.serviceModel>

 

Now  we can write http://xyz/CalulatorService.  

Neat isn’t it.

 

Conclusion

WCF 4.0 has addressed the pain points  of writing  tedious configuration details to host WCF services. It has  enhancements to configure, tracing, service discovery, routing, and .svc less activation services.WCF 4.0 promises to be the technology of choice for building scalable, service oriented, REST-based services. In this article we  had a look at the new features and enhancements in WCF 4.0 and saw the samples of  their  use in applications.

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

Posted by: Veejoi on: 6/1/2012 | Points: 25
Hai Pratiksha saxena,

The "4.svc-less activation of REST services " is not executing, and getting "Configuration Error".
I just checked the code, it should be

<add relativeAddress="CalulatorService.svc" service="Calculator"/>

the "relativeAddress" must be our somename.svc and "service" must be our service contract class name.

Thanks for the given aritcle. all the best.
VijayReddypogu

Login to post response

Comment using Facebook(Author doesn't get notification)