Step towards Windows Communication Foundation Part-3
Basic program to develop WCF enable service with hosting as well as client to consume it:
In this article we will see how to develop our first WCF service without using any visual studio template. Follow the step given as follow. For some basics of WCF please refer previous articles.
Start new console application and provide name as STEPONESERVICE.
Before starting add a reference to two namespaces for getting WCF functionality to our console application i.e. System.ServiceModel and System.ServiceModel.dll.
As recommended by WCF define an Interface with rules inside it. To this interface add “ServiceContract” attribute. So that we can use this as a contract in WCF Endpoint & expose it to multiple clients.
string GetAuthorName(int _authid);
Implementing the interface i.e. created. This task has is nothing to do with WCF endpoint & other processing but for better support with respect to performance as well as other features WCF recommends some of the .Net object to use & also to avoid. Eg. It prefer to use Generic Classes instead of normal classes. Some features of WCF will not be compatible with not preferred objects. In our example we will create a class & implement the interface for functionality.
public string GetAuthorName(int _authid)
string _connection = “Data Source=server2;User Id=sa;Password=123;DataBase=CSAUTHOR”;
Console.WriteLine(“Request Made :” + DateTime.Now.ToString());
SqlConnection _conn = new SqlConnection(_connection);
string _sql = “Select Author_Name From Author Where Author_Id=” + _authid;
SqlCommand _cmd = new SqlCommand(_sql, _conn);
SqlDataReader _rdr = _cmd.ExecuteReader();
public void AddAuthor()
Once component is prepared then we have to define an Endpoint and Host it in WCF supported environment. One of the option to host WCF created service is to use a program written with “ServiceHost” class of WCF. In main part of our program create Endpoint and use “ServiceHost” class to start the service.
static void Main(string args)
//Creating instance of ServiceHost class for hosting our service
ServiceHost obj = new ServiceHost(typeof(Author));
obj.AddServiceEndpoint(typeof(IAuthor), new BasicHttpBinding(), “Http://localhost:8001/Author”);
obj.AddServiceEndpoint(typeof(IAuthor), new NetTcpBinding(), “net.tcp://localhost:8002/Author1”);
Console.WriteLine(“Service Started. Press Any Key To Exit”);
Consuming WCF Service:
WCF supports many different ways of consuming a service. The basic distributed programming principle of creating proxy and communication with service is the most preferred way to access the service.
Proxy is also a class built in client language which act as a service to client. It has all method that a service provides inside it and code to convert this call to remote call.
In WCF we have classes that create proxies programmatically mean at runtime. This is very good for some logics but very complex to handle. In program “ChannelFactory” is one class which create proxy based on provide URL and contract info channel is a term which we use to refer a socket that we build with server. ChannelFactory is a class which can process all WCF channels.
ChannelFactory creates a class at runtime not at designtime. It requires endpoint info(A,B,C). In our exam we will take ServiceContract in client program which is actually build using endpoint. For starting our client program take a look step-by-step.
Start the console application provide the name as STEPONECLIENT.
Add servicecontract which we have built in service i.e. interface.
string GetAuthorName(int _authid);
In main write program to create instance ofchannelfactory by passing endpoint information.
//Create Instance of ChannelFactory Class
ChannelFactory<IAuthor> obj = new ChannelFactory<IAuthor>(new BasicHttpBinding(), new EndpointAddress(“Http://localhost:8001/Author”));
Once ChannelFactory is configured with required information then established the socket with server using CreateChannel method of it.
IAuthor reg = obj.CreateChannel();
Once socket is built then start invoking methods and display results.
int _authid = int.Parse(Console.ReadLine());
In this article we have seen the basic program to create WCF service with self hosting using ChannelFactory class.