Joydip Kanjilal
Contributor

New features in WCF 4.5

opinion
Dec 23, 20154 mins
Software Development

Take advantage of the new features in WCF 4.5 to build secure, reliable, and scalable services seamlessly

WCF (Windows Communication Foundation) provides a unified programming model for building scalable, robust services. It is a secure, reliable, and scalable messaging platform for developing service oriented applications in .Net. You can also use WCF to build RESTful services.

WCF has matured a lot over the years, and the latest stable release, WCF 4.5 comes up with many significant improvements. The salient new features and enhancements in WCF 4.5 at a quick glance:

  • Support for Web Sockets
  • Support for UDP endpoints
  • Simplified configuration files
  • Support for validation of WCF configuration
  • Support for Contract – First Development
  • Improvements in Streaming

Support for Web Sockets

Web sockets provide support for asynchronous full duplex communication using the HTTP protocol. In WCF 4.5, the netHttpBinding and netHttpsBinding have been have been updated to provide support for WebSockets. If you would like to set up support for WebSockets in your WCF service, you should set a CallbackContract on your ServiceContract. Here’s how you can achieve this.

[ServiceContract(CallbackContract = typeof(ITestCallback))]

public interface ITestService

    [OperationContract(IsOneWay = true)]

    Task SendTextMessage(string text);

}

Support for UDP endpoints

There are a couple of ways to exchange data between two or more processes over a network. You can use either TCP (Transmission Control Protocol) or the UDP (User Datagram Protocol) transport protocol. While the TCP is a secure and reliable connection oriented protocol, the UDP is a relatively less secure or reliable, fast, and connectionless protocol. WCF 4.5 now provides support for UDP protocol — a connectionless, lightweight protocol. The following code snippet illustrates how you can define a UDP endpoint in WCF 4.5.

<endpoint address="soap.udp://localhost:8080/" binding="udpBinding" contract="IDG.IService" />

Simplified configuration files

You longer need to worry about the lengthy configuration files that are generated by WCF. When you create a WCF service using WCF 4.5, the configuration that is generated will just have the non-default binding metadata in it. Here’s what a typical configuration generated in WCF 4.5 looks like:

<System.serviceModel>

<bindings>

<wsHttpBinding>           

        </wsHttpBinding>

</bindings>

<client>

<endpoint name="IDGBinding"

  address="http://localhost:8080/IDG/ITestService.svc"

  binding="wsHttpBinding"

  bindingConfiguration="WSHttpBinding_ITestService"

  contract="ITestService" />

</client>

</System.serviceModel>

Support for validation of the WCF configuration

This is a great new feature introduced in WCF 4.5. With WCF 4.5, there’s much improved support for intellisense in your popular Visual Studio IDE. With WCF 4.5 if Visual Studio can detect any issue with the configuration setting of your WCF service at the time of compilation, it will display you warnings when you compile your service project. Note that in the earlier versions of WCF you didn’t have any support for validation of the configuration settings of your WCF service.

Contract-first development

WCF 4.5 now provides support for contract first development — you can now generate the data contracts from the WSDL. You now have a contract-first tool integrated into Visual Studio that you can leverage to take advantage of this feature. Note that the popular tool svcutl now comes up with a new switch (the new switch is /serviceContract) using which you can generate service and data contracts from a given WSDL document.

Improvements in streaming

Asynchronous programming is a great feature to improvement the performance of your application. You can take advantage of asynchrony to perform operations sans the need to block the main or the executing thread of the application.  WCF 4.5 provides support for asynchronous streaming. Unlike in the earlier versions of WCF, your service will now not block while a service consumer is receiving the message. Rather, the thread will be made available to serve other service consumers. Now your WCF services that have been hosted in IIS can start processing the data even before the entire data has been received.

To enable this feature, you should specify the following in the service configuration.

<endpointBehaviors>

<behavior name = "IDGBehavior">

  <dispatcherSynchronization asynchronousSendEnabled = "true" />

</behavior>

</endpointBehaviors>

Joydip Kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author