Tuesday, August 25, 2009

WCF 4.0 Beta - Service Configuration Enhancements

 

Introduction

 

The .Net Framework 4.0 beta1 has shown lots of improvements in the area of Windows Communication Foundation. The new version has simplified the developer experience. It also provides us a rich integration with Workflow foundation. On top of all the improvements done one thing stands apart.

That is nothing but easier configuration of your services.

 

List of Configuration Improvements

1.      File less activation

2.      Default Endpoint

3.      Default Protocol Mapping

4.      Default Binding Configurations

5.      Default Behavior Configurations

 

File – less  Activation

 

In .Net 3.5 we were using .svc files to expose the services. We can configure the .svc extension by using IIS filter with little complexity.  Now .Net 4.0 introduces file-less activation for the services. What they mean by this is “There is no more .svc files”.

Following configuration will set you free from .svc files.

<configuration>

  <system.serviceModel>

    <serviceHostingEnvironment>

      <serviceActivations>

        <add relativeAddress="/MyPath" service="MyService"/>

      </serviceActivations>

    </serviceHostingEnvironment>

  </system.serviceModel>

</configuration>

 

Now we can browse the site like http://localhost/MyIISSite/MyPath.

 

Isn’t it cool ?

 

Default Endpoint

 

In WCF 3.5 / 3.0 if we host WCF service without endpoints it will throw error. Isn’t it ?

In .net 4.0 it is no longer an issue. In the runtime, default endpoints will be added

When the open method of ServiceHost is called, it will check the configuration file for the number of endpoints. If it is zero it will call the public method AddDefaultEndPoints to add the endpoint. One endpoint will be added per base address per service.

 

Default Protocol Mapping

 

WCF 4.0 allows you to map the transport protocol schemes and the WCF bindings. There are two ways to map the same. One in machine.config and the other by overriding  application configuration file.

In Machine.Config

    <protocolMapping>

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

        <add scheme="net.tcp" binding="netTcpBinding"/>

        <add scheme="net.pipe" binding="netNamedPipeBinding"/>

        <add scheme="net.msmq" binding="netMsmqBinding"/>

    </protocolMapping>

 

In App.Config

<system.serviceModel>

    <protocolMapping>

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

           bindingConfiguration="secureConfiguration"/>

    </protocolMapping>

  </system.serviceModel>

 

This will be useful if  in my application I am always going to use wshttpbinding rather than basic httpbinding for http protocol.

 

 

Default Binding Configurations

 

In WCF 3.5/3.0 we will define a named binding configuration which will be used against any endpoints. The problem here is we need to specify the configuration properly in the endpoints which may be cumbersome when we have more endpoints.

In WCF 4.0 we no longer have the name attribute. This will be considered as default and used by all default endpoints

<configuration>

  <system.serviceModel>

    <bindings>

      <basicHttpBinding>

        <binding messageEncoding="Mtom"> <!—No Name attribute -->

        </binding>

      </basicHttpBinding>

    </bindings>

  </system.serviceModel>

</configuration>

 

Default Behavior Configurations

 

Similar to Binding Configurations , WCF 4.0 supports default behavior Configurations.

<configuration>

  <system.serviceModel>

    <behaviors>

      <serviceBehaviors>

        <behavior> <!-- notice no name attribute -->

          <serviceMetadata httpGetEnabled="true"/>

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

</configuration>

 

Conclusion

 

WCF 4.0 provides more features apart from configuration enhancements which will reduce the developer burden as well as provide more opportunities to use WCF in new environments.

The configuration changes made in WCF 4.0 is for sure to make a difference to the developer / IT department world as they are the more concerned users.

No comments:

Post a Comment