WCF

WCF is abbreviation of Windows Communication Foundation. WCF is a framework used to build, configure, and deploy network-distributed services. Earlier it was known as Indigo. It enables hosting services in any type of operating system process. WCF has a combined features of Web Service, Remoting, MSMQ and COM+. WCF provides a common platform for all .NET communication.

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

Windows Communication Foundation (WCF) enables applications to communicate whether they are on the same computer, across the Internet, or on different application platforms.

WCF_MSA_Technosoft

Microsoft defines WCF as “a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application. An endpoint can be a client of a service that requests data from a service endpoint. The messages can be as simple as a single character or word sent as XML, or as complex as a stream of binary data.”

Features of WCF

  • Service Oriented
  • Interoperability
  • Multiple Message Pattern
  • Service Metadata
  • Data Contracts
  • Security
  • Multiple Transports and Encodings
  • Reliable and queued messages
  • Durable messages
  • Transactions
  • Support for AJAX and REST
  • Extensibility

WCF Architecture

The diagram below describes the basic architecture of Windows Communication Foundation:

wcf-architecture-MSA-Technosoft

Contract

Contracts layer define various aspects of the message system. It has four components:

The Data Contract describes every parameter that makes up each message that a service can create or consume. The Message Parameters are defined by XML Schema definition language (XSD) documents. It enables any system that understands XML to process the documents.

The Message Contract defines specific message parts using SOAP protocols. It allows finer-grained control over parts of the message, when interoperability demands such precision.

The Service Contract specifies the actual method signatures of the service. It is distributed as an interface in one of the supported programming languages, such as Visual Basic or Visual C#.

Policies and Bindings stipulate the conditions required to communicate with a service. Policies include security requirements and other conditions that must be met to communicate with a service.

Service Runtime

Service Runtime layer contains the behaviours that occur only during the actual operation of the service. Throttling controls how many messages are processed. It may vary if the demand for the service grows to a preset limit. Error specifies what occurs when an internal error occurs on the service. Metadata governs how and whether metadata is made available to the outside world. Instance specifies how many instances of the service can be run. Transaction enables the rollback of transacted operations if a failure occurs. Dispatch is the control of how a message is processed by the WCF infrastructure. Extensibility enables customization of runtime processes. Parameter filtering enables pre-set actions to occur based on filters acting on message headers.

Messaging

Messaging layer is composed of channels. A channel is a component that processes a message in some way. Channels operate on messages and message headers. There are two types of channels: Transport channels and Protocol channels.

Transport channels read and write messages from the network. Some transports use an encoder to convert messages, represented as XML Info sets, to and from the byte stream representation used by the network. Examples of transports are HTTP, named pipes, TCP, and MSMQ. Examples of encodings are XML and optimized binary.

Protocol channels implement message processing protocols, often by reading or writing additional headers to the message. Examples of such protocols include WS-Security and WS-Reliability.

Message layer illustrates the possible formats and exchange patterns of the data. WS-Security is an implementation of the WS-Security specification enabling security at the message layer. The WS-Reliable Messaging enables the guarantee of message delivery. Encoders present a variety of encodings that can be used to suit the needs of the message. HTTP specifies that the Hypertext Transport Protocol is used for message delivery. TCP specifies TCP protocol is used for message delivery. Transaction Flow governs transacted message patterns. Named Pipe enables inter-process communication. MSMQ enables interoperation with MSMQ applications.

Hosting and Activation

A service is a program. It must be run in an executable. This is known as a self-hosted service. Services can also be hosted, or run in an executable managed by an external agent, such as IIS or Windows Activation Service (WAS). WAS enables WCF applications to be activated automatically when deployed on a computer running WAS. Services can also be manually run as executable (.exe) files. A service can also be run automatically as a Windows service. COM+ components can also be hosted as WCF services.

WCF vs. Web Services

WCF-vs-Web-Services-MSA-Technosoft

Web Service is a legacy technology while WCF is latest service technology. Here are some comparisons:

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

 

Creating WCF Service | WCF Example

To create WCF Service follow the steps below:

Step1: Open Visual Studio in your PC.

Step2: Select File > New > Project.

Step3: Select Visual C# > WCF > WCF Service Application. Name your application click OK.

[NOTE: It will create two class files: Interface (IMyApplicationService.cs) & Class (ApplicationService.cs)]

Step4: Interface class code (IApplicationService.cs)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace ApplicationService {

[ServiceContract]

Public interface IApplicationService

{

[OperationContract]

string GetMessage(string name);

}}

Step5: Class file code (ApplicationService.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace ApplicationService {
public ApplicationService : IApplicationService {
public string GetMessage(string name)
{
return “Hello” + name;
}}}

Step6: Create Host

Add a Console Application > Rename it “ApplicationServiceHost” > Add Reference “System.ServiceModel” > Add Item > Application Configuration file(App.config) > Add reference of ApplicationService to ApplicationService itself

App.config code

< ?xml version= “1.0” encoding= “utf-8” ?>
<configuration>
<system.serviceModel>
<Services>
<service name= “ApplicationService.ApplicationService” behaviourConfiguration= “AppBehaviour”>
<endpoint address= “ApplicationService” Binding= “basicHttpBinding” Contract= “ApplicationService.IApplicationService”></endpoint>
<endpoint address= “ApplicationService” Binding= “netTcpBinding” Contract= “ApplicationService.IApplicationService”></endpoint>
<endpoint address= “mex” Binding= “mexHttpBinding” Contract= “IMetadataExchange”></endpoint>
<host>
<baseAddresses>
<add baseAddress= “http://localhost/8080/” />
<add baseAddress= “net.tcp://localhost/8090/” />
</baseAddresses>
</host>
</service>
</Services>
<behaviours>
<serviceBehaviours>
<behaviour name= “AppBehaviour”>
<serviceMetadata httpGetEnabled= “true” />
</behaviour>
</serviceBehaviours>
</behaviours>
</system.serviceModel>
</configuration>

Step7: Open Host; code for “Program.cs” in host application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace ApplicationServiceHost
{
Class Program
{
static void main()
{
using (ApplicationServiceHost MyHost= new ApplicationServiceHost( typeof(ApplicationService.ApplicationService)))
{
MyHost.Open();
Console.WriteLine(“Host has started working at ” + DateTime.Now.ToString());
Console.ReadLine();
}}}}

Step8: Set host application as start-up project and run to check whether it is working fine or not.

Step9: Create Client application( with http)

Open another instance of visual studio > File >new > project > empty web application > rename it to “MyWebClient” > Ok

Step10: Add Service reference

Right click on Reference > Add Service Reference > specify the address “http://localhost/8080/” > Go > rename service reference >OK

Step11: Add a web form > add a label, a text box and a button control to the web form > generate click event code for the button

namespace MyWebClient
{
public partial class webForm1: System.Web.UI.Page
{
protected void Button1_click( object sender, EventArgs e)
{
ApplicationService.MyWebClient c1= new ApplicationService.MyWebClient(“BasicHttpBinding_IApplicationService”);
Label1.Text = c1.GetMessage(TextBox1.Text);
}}}

Step12: run the application. Whatever you are going to type on text box, it will be written with Hello prefix as Label text.

 

Was this article helpful? Must share your views on comment section below.

For more informative technology related article, don’t forget to visit our Tech-Blogs

 

1 Comment

Lilly · October 5, 2018 at 10:09 PM

Nice post. I was checking continuously this blog and I am impressed! Very useful info specially the last part 🙂 I care for such information a lot. I was seeking this certain info for a long time. Thank you and best of luck.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.