Wednesday, October 8, 2008

Creating multiple channels using ChannelFactory in WCF

The System.ServiceModel.ChannelFactory class creates channels of different types that are used by clients to send messages to variously configured service endpoints. The channelFactory class is useful when you want share a common service contract dll between the client and the server. In this post I will show how to create a ChannelFactory implementation using WCF.

  • Create a new ClassLibrary project and add references to System.ServiceModel and System.Runtime.Serialization namespaces.
  • Create a service contract and data contract in the newly created project

[ServiceContract]

public interface IWCFService

{

[OperationContract]

string SayHello(Employee emp);

}

[DataContract(Namespace = "ChannelFactoryTest", Name = "Employee")]

public class Employee

{

[DataMember]

public int Id { get; set; }

[DataMember]

public string Name { get; set; }

public override string ToString()

{

return this.Name;

}

}

  • Create the class that implements the IWCFService interface

public class WCFService : IWCFService

{

public string SayHello(Employee emp)

{

return "Hello " + emp.ToString();

}

}

You can use this service contract and Data Contract created from both the Client and Host application for the service.

  • Create a new Console Application and name it WCFHost and add references to the already created project along with System.ServiceModel and System.Runtime.Serialization assemblies.
  • In the main method add the following lines to run the application as a Service Host

static void Main(string[] args)

{

Uri[] baseAddresses = new Uri[] { new Uri("net.tcp://localhost:8001/") };

using (ServiceHost host = new ServiceHost(typeof(WCFService), baseAddresses))

{

host.Open();

Console.WriteLine("Service runnning...");

Console.ReadKey();

host.Close();

}

}

  • Add a new App.Config file and add the following lines to the configuration Section of the application configuration file.

<configuration>

<system.serviceModel>

<services>

<service name="ChannelFactoryTest.WCFService">

<endpoint address="WCFService" binding="netTcpBinding" contract=" ChannelFactoryTest.IWCFService" />

service>

services>

system.serviceModel>

configuration>

  • Create another Console application in the same solution and name it WCFClient and add references to the ChannelFactoryTest project and System.ServiceModel and System.Runtime.Serialization assemblies.
  • Add an application configuration file and add the following lines to the configuration Section of the application configuration file.

<configuration>

<system.serviceModel>

<client>

<endpoint name="WCFServiceConfiguration" address="net.tcp://localhost:8001/WCFService" binding="netTcpBinding" contract=" ChannelFactoryTest.IWCFService" />

client>

system.serviceModel>

configuration>

  • Change the main method of the client to use a ChannelFactory to create a proxy for using the service.

static void Main(string[] args)

{

Console.WriteLine("Press any key to start...");

Console.ReadKey();

ChannelFactoryTest.IWCFService proxy = new ChannelFactory< ChannelFactoryTest.IWCFService>IWCFService>("WCFServiceConfiguration").CreateChannel();

ChannelFactoryTest.Employee emp = new ChannelFactoryTest.Employee { Id = 1, Name = "Prajeesh" };

Console.WriteLine(proxy.SayHello(emp));

((IChannel)proxy).Close();

Console.ReadLine();

}

No comments: