Archive for the ‘Technical Articles’ Category

Windows Communication Foundation

April 29, 2018

WCF Architecture

WCF stands for Windows Communication Foundation. It is a framework for building, configuring, and deploying network-distributed services. Earlier known as Indigo, it enables hosting services in any type of operating system process.

The elementary feature of WCF is interoperability. It is one of the latest technologies of Microsoft that is used to build service-oriented applications. Based on the concept of message-based communication, in which an HTTP request is represented uniformly, WCF makes it possible to have a unified API irrespective of diverse transport mechanisms.

WCF was released for the first time in 2006 as a part of the .NET framework with Windows Vista, and then got updated several times. WCF 4.5 is the most recent version that is now widely used.

A WCF application consists of three components −

  • WCF service,
  • WCF service host, and
  • WCF service client.

WCF platform is also known as the Service Model.

Fundamental Concepts of WCF

Message

Message instances are sent as well as received for all types of communication between the client and the service.

Endpoint

It defines the address where a message is to be sent or received. It also specifies the communication mechanism to describe how the messages will be sent along with defining the set of messages. A structure of an endpoint comprises of the following parts −

  • Address − Address specifies the exact location to receive the messages and is specified as a Uniform Resource Identifier (URI). It is expressed as scheme://domain[:port]/[path]. Take a look at the address mentioned below −

net.tcp://localhost:9000/ServiceA

Where

net.tcp = is the scheme for the TCP protocol.

Localhost = is the domain which can be the name of a machine or a web domain.

ServiceA = it the path.

  • Binding − It defines the way an endpoint communicates. It comprises of some binding elements that make the infrastructure for communication. For example, a binding states the protocols used for transport like TCP, HTTP, etc., the format of message encoding, and the protocols related to security as well as reliability.
  • Contracts − It is a collection of operations that specifies what functionality the endpoint exposes to the client. It generally consists of an interface name.

Hosting

Hosting from the viewpoint of WCF refers to the WCF service hosting which can be done through many available options like self-hosting, IIS hosting, and WAS hosting.

Metadata

This is a significant concept of WCF, as it facilitates easy interaction between a client application and a WCF service. Normally, metadata for a WCF service is generated automatically when enabled, and this is done by inspection of service and its endpoints.

WCF Client

A client application that gets created for exposing the service operations in the form of methods is known as a WCF client. This can be hosted by any application, even the one that does service hosting.

Channel

Channel is a medium through which a client communicates with a service. Different types of channels get stacked and are known as Channel Stacks.

SOAP

Although termed as ‘Simple Object Access Protocol’, SOAP is not a transport protocol; instead it is an XML document comprising of a header and body section.

Advantages of WCF

  • It is interoperable with respect to other services. This is in sharp contrast to .NET Remoting in which both the client and the service must have .Net.
  • WCF services offer enhanced reliability as well as security in comparison to ASMX (Active Server Methods) web services.
  • Implementing the security model and binding change in WCF do not require a major change in coding. Just a few configuration changes is required to meet the constraints.
  • WCF has built-in logging mechanism whereas in other technologies, it is essential to do the requisite coding.
  • WCF has integrated AJAX and support for JSON (JavaScript object notation).
  • It offers scalability and support for up-coming web service standards.
  • It has a default security mechanism which is extremely robust.

There are some major differences that exist between WCF and a Web service which are listed below:

  • Attributes − WCF service is defined by ServiceContract and OperationContract attributes, whereas a web service is defined by WebService and WebMethod attributes.
  • Protocols − WCF supports a range of protocols, i.e., HTTP, Named Pipes, TCP, and MSMQ, whereas a web service only supports HTTP protocol.
  • Hosting Mechanisms − Various activation mechanisms are there for WCF hosting, i.e., IIS (Internet Information Service), WAS (Windows Activation Service), Self-hosting and Windows Service, but a web service is hosted only by IIS.
  • Services − WCF supports a robust security, trustworthy messaging, transaction and interoperability, while a web service only supports security services.
  • Serializer − WCF Supports DataContract serializer by employing System.Runtime.Serialization, whereas a web service supports XML serializer by making use of System.Xml.Serialization.
  • Tools − ServiceMetadata tool (svcutil.exe) is used for client generation for a WCF service, while WSDL.EXE tool is used for generating the same for a web service.
  • Exception Handling − In WCF, unhandled exceptions are handled in a better way by making use of FaultContract. They do not return to the client like in a web service as SOAP faults.
  • Hash Table − It is possible to serialize a Hash Table in WCF, but this is not the case in a web service.
  • Bindings − WCF supports several types of bindings like BasicHttpBinding, WSDualHttpBinding, WSHttpBinding, etc., while a web service supports only SOAP or XML.
  • Multithreading − WCF supports multithreading by using the ServiceBehavior Class, whereas this is not supported in a web service.
  • Duplex Service Operations − WCF supports duplex service operations apart from supporting one-way and request-response service operations, whereas a web service does not support duplex service operations.

WCF – Developers Tools

For developing a WCF service application, there are mainly two tools – Microsoft Visual Studio and CodePlex. There is also a readymade template in Visual Studio for developing WCF service application. Selection of such a template leads to addition of files for the following purposes −

  • Service contract
  • Service implementation
  • Service configuration

Requisite attributes get added automatically and a simple “Hello World” service gets created by Microsoft Visual Studio without even writing any code.

Some of the tools offered by CodePlex for developing WCF service applications are as follows:

  • blue − This is a Microsoft Visual Studio add-in, and also “contract-first” development toolset that facilitates the defining of the WCF service operations, and generating a code skeleton accordingly. An important link for the same is − https://wscfblue.codeplex.com
  • WCFProxyGenerator − This is also a Microsoft Visual Studio add-in. The tool is used to extend the client-side generation and offer additional error handling. For having more information regarding this particular development tool, visit https://wcfproxygenerator.codeplex.com
  • WCFMock − Testing of WCF service can be a complicating task and this development tool offers a convenient solution for the unit testing of WCF services by its useful classes. For more information regarding this tool, visit https://wcfmock.codeplex.com

NOTE

Another free tool for the development of WCF service applications in an easy manner is WCFStorm. Its LITE version offers many striking features for dynamically invoking and testing WCF services, editing the service binding, modifying WCF URL endpoint, etc.

Advertisements