WCF, windows communication foundation Introduction, ABC of WCF, sample wcf application

What is WCF? 
Windows Communication Foundation (WCF) is a structure that is used for supporting communication between two or more servers irrespective of their platform or technology.

 

Why WCF?
WCF is not restricted to any specific protocol or hosting or platform. WCF provides various options for developer to develop WCF services. Where as remoting and web services have limitations. Remoting and web services are subsets of WCF and WCF extends further to overcome those limitations.
 
After developing a WCF service that supporting some protocols and later if we want to make the same service to support another protocol then with out any changes in the service just by adding another endpoint we can achieve this.

WCF provides various hosting options, one can host a WCF service in IIS or windows service or self hosting or WAS hosting or can run as an application.
 
There are three building blocks in every WCF service. Those are Address, Binding and Contract, shortly known as WCF ABC or abc wcf.
 
Address: Every WCF service is associates with a unique address and the address contains two elements. One is location and another is transport schema. The location indicates the location of the WCF service (combination of host machine, application name and port number) and transport schema indicates the name of protocol. WCF supports http/https, TCP, IPC, Peer Network, Microsoft Message Queue (MSMQ) and Service bus.
 
Binding: Binding describes how to interact with the WCF service in terms of transport protocol, message encoding, communication pattern, reliability, security and interoperability.
 
Contract: In WCF service, Contract describes what that particular service do, what methods service provides and purpose of those methods.
 

Example:Sample WCF application in C# which illustrates How to create Basic WCF service and client application to consume the WCF service
 
WCF Service:
 
File => New project => select WCF Service Library template => give a name BasicWCFService
 
Above steps creates Iservice1.cs and service1.cs files
 
Open IService1.cs and code as per requirement

 

 

namespace BasicWCFService

    // NOTE: If you change the interface name "IService1" here, you  must also update the reference to "IService1" in App.config.

 

    [ServiceContract

    public interface IService1

    { 

        [OperationContract]

        string GetMessage();

 

        [OperationContract

        string AddMessage(string str);

 

        // TODO: Add your service operations here

    }

}


Now open Service1.cs and Implement above Interface


 

namespace BasicWCFService

    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config. 

    public class Service1 : IService1 

    { 

        public string GetMessage() 

        { 

           return "Welcome to WCF Service....!"

        }

 

         public string AddMessage(string str) 

         { 

            return ("string " + str + " added successfully....!"); 

        } 

    }

}

 

To Remove Test option:

 

Right click on project => properties => select Debug option => textbox beside command line arguments have “/client:"WcfTestClient.exe"” remove this text and save the service.


Now start Debugging à service is self hosted and ready to consume.


Double click on WCF service host icon in system tray and copy Metadata address; we have to give this address as add service reference in all client applications.


 

Client: Basic console application to consume the service.

 

File => New project => select Console application  => give a name BasicWCFClient

 

Right click on project => Add service Reference => give metadata address  => click on Go button

 

Above step will generate list of services => select the service for reference => namespace name ServiceReference1 => click Ok Button

Now start consuming the service.

using System.ServiceModel;

namespace BasicWCFClient

{

    class Program

    {

        static void Main(string[] args)

        {

            ServiceReference1.Service1Client obj = new   ServiceReference1.Service1Client();

            Console.WriteLine(obj.GetMessage());

            Console.WriteLine(obj.AddMessage("From client Program...."));

            Console.ReadKey();

        }

    }

}

 

Now start Debugging the client application.


 

                                                                                                           BasicWCFService.rar (14.81 kb)

                                                                                                            BasicWCFClient.rar (31.59 kb)