Sony Arouje

a programmer's log

WCF Hosting an easy method

with 2 comments

My current project is based on WCF service. One of my responsibility is to find a method to host the WCF service. I googled a lot and failed to find one. Every site mentioned about creating ServiceHost instance one by one and call the open method on all service. The disadvantage of this method is we need to add a ServiceHost instance when ever we add a new service to the hosting application. Thus by modifying the service host application

So I come across with an idea of a method to read the Service configuration and host each service. Following is the method I created.

using System.Xml.Linq;

private static List<Type> ServiceHostHelper()
{
    XDocument configXML = XDocument.Load(“ServiceHost.exe.config“); //load the app config file to read using LINQ.
    var configs = (from config in configXML.Descendants(“service”)
                       select new AssemblyDetails
                       {
                           ActivatorClass = config.Attribute(“name”).Value
                       }
                   );

    Assembly assembly;
    assembly = Assembly.LoadFrom(“AddressMngr.dll”);
    List<Type> assmTypes = new List<Type>();
    foreach (AssemblyDetails assm in configs)
    {

//reflection to get the type of the service.         
        Type type = assembly.GetType(assm.ActivatorClass);         assmTypes.Add(type);
    }
    return assmTypes;
}

AssemblyDetails is my class to store the name of the service class. Below is the class

public class AssemblyDetails
{
    string _activatorClass;
    string _fullPath;
    public string ActivatorClass
    {
        get { return _activatorClass; }
        set { _activatorClass = value; }
    }
    public string FullPath
    {
        get { return _fullPath; }
        set { _fullPath = value; }
    }
}

Now lets see how I host the service. Here I host service using a console application

static void Main(string[] args)
{
    List<Type> services = ServiceHostHelper(); // services object will have all the services we defined in the app.config

    ServiceHost[] hostApp = new ServiceHost[services.Count];
    Console.WriteLine(“Following Services are Started”);
    for (int i=0;i<services.Count;i++)
    {

            Type service = services[i];
            hostApp[i] = new ServiceHost(service);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(service.ToString());

            hostApp[i].Open();
    }
    Console.ForegroundColor = ConsoleColor.Gray;
    string key=string.Empty ;
    do
    {
        Console.Write(“Type exit to terminate the host>”);
        key = Console.ReadLine();
        if (key == “exit”)
        {
            for (int i = 0; i < services.Count; i++)
            {
                hostApp[i].Close();
            }
        }
    } while (key != “exit”);
}

We also need to see how I configured the service. Here is my app.config configuration

<services>
  <service name=”AddressMngr.UserManager“>
    <endpoint address=”net.tcp://localhost:8001/UserManager”
              binding=”netTcpBinding”
              bindingConfiguration=”TransactionalTCP”
              contract=”AddressMngr.IUser”/>
  </service>
  <service name=”AddressMngr.Address” behaviorConfiguration=”ThrottledBehaviour”>
    <endpoint address=”net.tcp://localhost:8001/Address”
      binding=”netTcpBinding”
      bindingConfiguration=”TransactionalTCP”
      contract=”AddressMngr.IAddress”/>
  </service>
</services>

One thing we need to take care is the service name (marked in red) should be same as the class we implemented the Service contract with full name space. I am giving the fully qualified name is to load the class using reflection.

If you have any question then let me know, am happy to help you.

Technorati Tags: ,
Advertisements

Written by Sony Arouje

August 25, 2010 at 4:17 pm

Posted in WCF

Tagged with ,

2 Responses

Subscribe to comments with RSS.

  1. Why don’t you use IIS to host your service ?

    Pradeep

    October 1, 2010 at 11:33 pm

    • IIS only support http or wshttp. But as per our design they should communicate in tcp or net pipe, so only option is custom hosting.

      sonyarouje

      October 2, 2010 at 5:52 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: