• Now Online : 30
  • admin@codemyne.net

Introduction

WCF is a unified programming model provided with .NET 3.0 for developing distributed application. Unified programming model means combining set of distributed technologies under one roof, with similar programming approach. WCF supports TCP and HTTP communication. WCF supports binary format, XML format and binary+XML fromat(MTOM format). MTOM is the process of compressing soap message. WCF supports different hosting environments. [The environment which will manage class as a WCF service is called as hosting environment].WCF provides builtin session. WCF provides builtin transaction management.

Hosting the WCF Service

1. Self Hosting: Hosting WCF service class with any .NET application is called self hosting.
Example: Console application, Windows Forms Application, Windows Service.
2. IIS Hosting: Hosting WCF service class with webserver IIS is called as IIS hosting.
3. WAS Hosting: Windows Process Activation Service Hosting.

Http communication, this is supported with IIS 7.0 or later WAS is a windows component which provides additional intelligence to webserver IIS to support TCP communication.
WCF service is a class with data and collection of operations(methods) with minimum one endpoint. Client application can communicate with WCF service based on endpoint. End point is a collection of three things 1. Address 2. Binding 3. Contract
Address will specify where WCF service is located. Ex: Net.tcp://servername:portnumber/servicename
Port number provides unique identification to service with operating system. Port number should be between 0 to 65535, in this 1024 ports are reserved, it is recommended to provide portnumber > 1023.
Binding will specify how WCF service provides communication to client, this includes message encoding formats and security protocol.

WCF supports different types of bindings:
1. NetTcpBinding: This provides tcp communication, it is similar to remoting.
2. Basichttpbinding: This provides http communication without WS-* specification support, it is similar to webservice.
3. Wshttpbinding: This provides http communication with WS-*specification support, it is similar to WS-* specifications(transactions, security,...)
4. Wsdualhttpbinding: This is similar to wshttpbinding, it support dual message pattern(Dual message pattern means WCF service can perform callback to client application method).
Contract will specify what WCF service provides to client application this includes methods (operations) given to client. Contract can be considered as an agrement between client application and wcf service regarding exchanging information. The information can be methods description or data description..so on.

WCF supports different types of contracts:
1. Service Contract: This provides information regarding list of operations (methods) supported by wcf service.
2. Data Contract: This provides information regarding data to be exchanged between client and wcf service.
3. Fault Contract: This provides information regarding exception occurred with in wcf service.

DotNet assembly to work with WCF:

DotNet is providing system.servicemodel.dll to work with WCF. system.servicemodel.dll provides following classes
1. Service Contract: Service contract will provide list of operations supported by WCF service to client application, this should be applied to interface implemented by WCF service class.
2. Operation Contract: Operation contract will provide WCF service method (operation) description to client application.
3. Data Contract: This should be applied to operations methods of service contract interface.

Steps to create WCF service:
Creating WCF service:
  • Creating service contract interface.
  • Creating class implementing service contract interface.
  • Hosting class as wcf service.
  • Applying endpoints to wcf service.
Creating client application to consume WCF service logic:
  • Creating wcf service with self hosting.
  • Goto visual studio.
  • Select new project.
  • Select visual C#.
  • Select windows forms application, name it as wcfmultiserver
  • Placing wcf service into project:
  • Goto project menu.
  • Select add new item
  • Select wcf service template, name-mysevice.cs
  • This will create three files into project
  • imyservice.cs --> It will contain service contract declaration.
  • myservice.cs --> It will contain class declaration.
  • app.config --> Contains endpoint declaration.
  • Goto imyservice.cs. and change the code as follows
[ServiceContract]
public interface ImyService1
{
    [OperationContract]
    int sum(int a, int b);
} 
--> Goto myservice.cs and change the code as follows
public class myService1 : ImyService1
{
    public int sum(int a,int b)
    {
        int res;
        res = a + b;
        return (res);
    }
}
--> Goto form1
--> Place two buttons and a label
--> button1, name-Host wcf service
--> button2, name-Stop wcf service
--> Goto form1.cs, and code as follows
public partial class Form1 : Form
{
    ServiceHost sh = null;    
    private void button1_Click(object sender, EventArgs e)
    {
        sh = new ServiceHost(typeof(myService1));
        sh.Open();
        label1.Text = "wcf service status:" + sh.State;
    }
                                                private void button2_Click(object sender, EventArgs e)
    {
        sh.Close();
        label1.Text = "wcf service status:" + sh.State;
    }
} 

Applying endpoints to wcf service:

This requires setting with in app.config. Visual studio is providing wcf service configuration editor to apply settings with in app.config using GUI approach, this makes developer job easier and faster.
Working with WCF service configuration editor:
  • Goto tools menu --> wcf service configuration editor.
  • Goto file menu, open config file
  • Expand myservice node and select host
  • Create new base address(delete the previous base address)
  • Click on delete button to remove the default address, click on new button and provide required address(net.tcp://localhostname:2000/myservice)
  • Expand endpoint node it will display 2 emptyname endpoints
  • Select first emptyname endpoint(default wcfhttpbinding)
  • --> Name - nettcpendpoint
    --> Binding - nettcpbinding
    --> Contract - wcfmultiserver.imyservice
  • Select second emptyname endpoint
  • --> Address - mex
    --> Binding - mextcpbinding(default is mextcpbinding)
    --> Contract - IMetadataExchange
  • File menu --> save and exit(Based on this settings, it will place XML tags into app.config)
  • Execute the project
Note: When you click on host wcf service button open method of service host class will make myservice class as wcf service by registering endpoints with operating system reading from app.config
WCF server class is available to clients within network with the url as "net.tcp://servername:2000/myservice".
When you click on close wcf service, close method of servicehost class will remove endpoints of wcf service from operating system, wcf service class will not be available to clients.
Mexpoint VS Endpoint: Mexpoint will provide type metadata of wcf service to client for generating proxy class(type metadata includes complete description of wcf service)
Endpoint will provide data and methods execution to client application.
If wcf service is not supporting http communication then httpgetenabled should be set to false.

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment

Chandramohan 8/30/2012 (IST) / Reply

Simply Superb Definition of WCF. I really like it.

chandu 8/31/2012 (IST) / Reply

Why cant you provide more examples on wcf?