Basic Programming
This article gives you the clear picture of WCF implementation
Introduction
Windows Communication Foundation (WCF) is an SDK for developing and deploying services on Windows.
WCF provides a runtime environment for services, enabling you to expose CLR types as services, and to consume other services as CLR types.
Service: the functionality exposed to the world
Client: consuming the functionality from the service.
Main Components
- Endpoint
- Hosting environment
- Service class
The Endpoint in WCF Is ABC
- A- Address (where)
- B-Binding (How)
- C-Contract
WCF supports following transport schemas
- HTTP
- TCP
- Peer network
- IPC (Inter-Process Communication over named pipes)
- MSMQ
Contracts in WCF
o Service Contract (at class level)
o Operation Contract(atmethod level)
o Data contract (at class level)
o Data Member (at property level)
- Fault Contract
- Message Contract
Hosting of WCF
- IIS
- Self Hosting
- WAS (Windows Activation Service)
Implementation
STEPS FOR WCF IMPLEMENTATION
- Console application
- Add reference system.serviceModel.dll
- Include the namespace System.ServiceModel;
Server Side - create a service contract
- [ServiceContract (Namespace=http:
- Create an Interface
- Public interface Icalc
- {
- …….
- ….
- }
- Declare an method of each operation with OperationContract attribute
- [ServiceContract (Namespace=http:
- Public interface Icalc
- {
- [OperationContract]
- Int add(int n1,int n2);
- [OperationContract]
- Int Sub(int n1,int n2);
- }
- Create an another class which implements the interface
- Public class cal_op:ICalc
- {
- Public int add(int a,int b)
- {
- Return(a+b);
- }
- Public int sub(int a,int b)
- {
- Return(a-b);
- }
- }
In main program:
- Config. The address
- Uri baseaddress=new uri(http:
- Create a service host
- ServiceHost selfhost=new ServiceHost(typeof(class_name),<object of uri >);
- ServiceHost selfhost=new ServiceHost(typeof(cal_op),baseAddress);
- Add service end point
- Selfhost.AddServiceEndPoint(typeof(<interface name>),new WSHttpBinding(),”<service name>”);
- Selfhost.AddServiceEndPoint(typeof(Icalc),new WSHttpBinding(),”calc_server”);
- Hosting with Metadata
- ServiceMetadataBehavior smb=new ServiceMetadataBehavior();
- Smb.HttpGetEnabled=true;
- Selfhost.Description.Behaviors.Add(smb);
- Then open the host
- <host_name>.open();
- Selfhost.Open();
-
- …
- …
- Selfhost.Close();
-
-
- In VS2008 CMD:
- Svcutill.exe/language:cs/out:<filename.cs>/config http:
- Svcutill.exe/language:cs/out:client.cs>/config http:
- Add that file to client solution
Client side:
Create an end point by using tat obj
- <servicename> obj=new <servicename>();
- Console.WriteLine(“{0}”,obj.Add(10,5));