Using WCF for providing web service

WCF (Windows Communication Foundation) is a technology for communication built in the framework of .Net 2.0. It combines the communication services of .Net which are remoting, MSMQ, Web services and COM+ that were released in the earlier versions.

WCF is based on the principle of SOA (Service Oriented Architecture). It has been mainly designed to provide better interoperability and service orientation that helps to build manageable applications. It satisfies the various communication requirements for the current web-based applications like performance, interoperability, security and transaction support. It is built such that it provides smooth transition for a developer to build code in a service oriented approach from the traditional object oriented methodology.

Overview on WCF

WCF consists of runtime and a set of APIs to facilitate development of systems to communicate with other web applications. A WCF service consists of endpoints which define what messages to be sent, where and how to be sent, etc. This information called metadata, is used to help design the clients for availing a WCF service. An endpoint contains the following:

Address:: where the service is located

Binding : how to communicate with the service

Contract:: operation that service can execute

WCF provides different bindings needed for communication, some of which are defined as:

BasicHttpBinding: Communication through SOAP over HTTP to connect to Web services that conform to WS-I Profile specification

WsHttpBinding: Communication with Web services and clients that support the WS-* protocols over HTTP

NetTCPBinding: Reliable, secure and high performance communication between two WCF entities over TCP

NetNamedPipesBinding: WCF-to-WCF communication between processes residing on same Windows based systems

NetMsmqBinding: Communication between two software entities in WCF using MSMQ

WCF allows different mechanisms for encoding the message that specifies how it is formatted. Following are the standard options, in addition to the customized option that can be used with the help of built-in extension points:

• Text encoding – used for interoperability
• MTOM (Message Transmission Optimization Mechanism) – Message encoded in unstructured binary data exchanged between client and server in interoperable scenarios
• Binary encoding – used between WCF entities for better performance


WCF supports different types of contract called as Message patterns for interaction between systems and they are as below:

One-way: One-way communication, client does not wait for the response and hence methods of this type return void.

Duplex: Two-way communication where the client initiates a call on the service and also provides a callback URI so that the server notifies the client through the callback channel after service.

Request-Reply: Client expects a reply for the method called on the server which usually returns values correlated to the request as per the terms of the contract

Specifications of WCF

WCF adheres to all the specifications defined by WS-*. These are the standards defined by group companies like IBM, Microsoft, etc. for exposing a service in the web through a common protocol. Due to its adherence, WCF is compatible with other languages (like Java) and leverage the features provided in languages of .Net like C# & VB.net.

Following are the specifications:

Messaging (WS-Addressing) – helps to make SOAP to be independent of underlying protocol for carrying address information

Security – (WS-Security, WS_Trust and WS-Secure Conversation) – define authentication, data integrity and privacy features for the service

Reliability (WS-Reliable messaging) – Ensures end-to-end communication when multiple Web services are involved during message traversal

Transactions (WS-Co-ordination and WS_Atomic Transaction) – Enables transactions that occur during web conversations

Metadata (WS-Policy and WS-Metadata exchange) – Information about a web service that specifies dynamic behavior and that cannot be stated in WSDL form is expressed as metadata

WCF provides better tools for deploying and maintaining applications like configuration editor, trace viewer, message logging tool, a vast number of performance counters and support for Windows Management Instrumentation.

Following are the different ways of hosting WCF service:

• Self-hosting which means service exists as long as the Service host class exists
• Hosting in application domain or in a process executing in IIS server
• Hosting in application domain or in a process executing in WAS (Windows Activation Service) server

WCF, thus provides a layered architecture for building distributed applications with many options for encoding, transport, message pattern, etc. Hence, while designing a WCF service, care needs to be taken to select the suitable option for satisfying the given requirement. For example, if performance is the main concern, the message content can be in SOAP format but encoded and communicated in binary form for transport. Also, decision to migrate from existing technology (remoting, ASP.net web services, etc.) to WCF should be made based on the need to leverage its features such as interoperability, security, transaction support and better management facilities.

To host a web service using WCF, following are the steps need to be followed:

a) Define the service contract which specifies the operations that the service supports. Each contract is created by defining an interface (in C or Visual Basic or C++) and each method in it represents the service operation. Each interface must have ServiceContractAttribute applied to it and each operation must have OperationContractAttribute applied so that they are exposed.

b) Implement the contract by defining a class that implements the interface with the code for executing the business logic of the contract.


c) Host (in ‘self-hosting’ mode) and run the service as specified in the following steps:
a. Configure base address for the service which implies the URI of the service from where it can be accessed from (through code or configuration in web.config file)
b. Create a service host (a new instance for ServiceHost type) to host this service
c. Add an endpoint to the service host to expose the service
d. Enable metadata exchange to get information about the client (whether using WCF and other data)
e. Open the service host to service the requests from the clients

Benefits of WCF:

• Adherence to WS-* specifications gives the ability to integrate with applications written in other languages like JAVA and C++ along with high level of interoperability
• Provides extensibility by allowing changing the type of protocol and serialization easily
• Ability to build secure and robust web service in a service-oriented approach
• Compatibility with applications designed in the earlier web technologies of Microsoft
• Does not require the existence of IIS for hosting the service
• Saves time and cost of development of complex distributed applications

WCF is a right concept introduced by Microsoft to meet the current expectations of development of distributed applications in terms of several requirements for the agile business needs.

|How to Define Custom Error Pages for Error Handling In ASP.Net Application| Role of Web gardens and web farms in ASP.NET | Understanding Purpose and Usage of "event" Class Member Modifier in C# (C Sharp) | Understanding the Structure and Content of Web.Config File | Usage of C# (C Sharp) Query Keywords – group, by, into | Usage of C# (C Sharp) Query Keywords – join, on, equals, let |Usage of C# (C Sharp) Query Keywords – orderby, ascending, descending | Usage of C# (C Sharp) Query Keywords – select, from, where, in | Usage of Lambda Operator (=>) in C# (C Sharp) |Using C# (C Sharp) as a tool for object oriented programming | Using Dictionaries in .Net – An Overview | Using WCF for providing web service |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.