Month: October 2013

New Features in WCF 4.5

Posted on Updated on

With the release of Windows Communication Foundation (WCF) in version 3.0 of the .NET Framework in 2006, Microsoft set the stage for a unified framework for developing service-oriented applications in .NET. Since then, I have personally become quite a fan of WCF and have created countless services and service clients. WCF is flexible and robust, and with .NET 4.5 has become even better!

Though we won’t dive into detail, I would like to take a quick moment to briefly discuss some of the highlights of the new features in WCF 4.5. These are the new features that I have found to be most exciting:

Generated Configuration Files have been Simplified

When we Add a Service Reference via Visual Studio or use the svcutil.exe, a client config file is generated for us. In earlier versions, this file contained every binding present, including those that just had default values. Now the config files only contain those with non-default bindings.

Configuration Validation

When we build our project(s) via Visual Studio, the attributes that we implement within our project classes are validated against the values in the config files. This allows us to discover issues at built-time without having to wait for run-time.

ASP.NET Compabibility Mode Default

Through use of the aspNetCompatibilityEnabled attribute in the <serviceHostingEnvironment> node, WCF now gives developers access to the functionality of the HTTP pipeline when writing WCF services. Setting this to true enables us to access the HttpContext.

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

Support for Asynchronous Streaming

WCF now truly supports asynchronous streaming – this is a significant scalability enhancement!

IIS HTTPS Simplification

A new HTTPS protocol mapping which greatly simplifies implementing a WCF server over HTTPS when using IIS. I remember this being a slight pain in the past.

<system.serviceModel>
    <services>
         <service name="SampleService">
               <endpoint address=""
                     binding="basicHttpsBinding"
                     contract="ISampleService">
              </endpoint>
         </service>
    </services>
 </system.serviceModel>

Prior to WCF 4.5, we would have had a <basicHttpBinding> node and a <security> node with the mode attribute set to “Transport”. Now we just set a basicHttpsBinding attribute within the endpoint. Pretty cool!

Easier Configuration of WCF Services in Code

In the pre-4.5 days, we had to create a ServiceHostFactory that created the ServiceHost. in WCF 4.5, you merely create a public static method named Configure that accepts a ServiceConfiguration object.

public class MyService : IMyService
{
    public static void Configure(ServiceConfiguration config)
    {
        ServiceEndpoint serviceEndpoint = new ServiceEndpoint(new ContractDescription(&quot;IMyService1&quot;),
                new BasicHttpBinding(), new EndpointAddress(&quot;basic&quot;));
        serviceEndpoint.Behaviors.Add(new MyEndpointBehavior());
        config.AddServiceEndpoint(serviceEndpoint);

        ServiceDebugBehavior sdb = new ServiceDebugBehavior();
        sdb.HttpGetEnabled = true;
        config.Description.Behaviors.Add(sdb);

        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        smb.IncludeExceptionDetailInDefaults = true;
        config.Description.Behaviors.Add(smb);
    }
}

Multiple Authentication Types via a Single Binding

You read that right! Beginning with WCF 4.5, multiple authentication types can be used via a single endpoint if you are using HTTP with Transport security. Because IIS allows multiple authentication methods on a virtual directory, these same authentication types can be used by WCF for the virtual directory in which the web service is hosted.

WebSocket Support via Two New Bindings

Two new bindings, NetHttpBinding and NetHttpsBinding provide TCP-like performance over the standard HTTP and HTTPS ports (80 and 443) bidirectionally. The HTTP paradigm that we all know and love has been built based on the concepts of requests and responses. The WebSocket specification defines socket connections between a web browser and a server over HTTP. This is a wonderful enhancement because what we have is a connection between the client and the server and both parties can start sending information any time. A discussion of WebSockets is beyond the scope of this short feature overview, but it is a topic worth discussing at a later date.

UDP Support

Anyone familiar with UDP knows that it is a great way to very quickly send messages in one direction. WCF 4.5 now supports UDP!

Conclusion

Though there are other exciting enhancements in WCF 4.5, these are my favorites and in my mind some of the most interesting.