Host a service in a managed application

I think I have mentioned it before (if I haven’t, I’ll surely do now) that WCF isn’t about web services. It’s about services, as a whole. There are no restrictions on where and how you’d like to host your service. Given this, I’ll show three easy and quick ways in this post to host your WCF services in managed applications:

  • Host with an application having a UI
  • Host with a Windows Service
  • Host with the WCF-provided host

The first two options are essentially the same, the only difference is that Windows Services don’t have a user interface, and they have their own ways of installation and running. But let’s treat them as separate objectives.

We’ll use the simplest form of applications, the Console Application to self-host our WCF service. So if you’re ready (created a console app), add the necessary code files to it (the service class, and the service contract interface, plus an app.config). I’m sure you’ll be able to write a simple service by now, and the whole purpose of this post is to show you how to host your written services, instead of how to write them, so I’ll omit the creation of a service. Let’s assume you’ve a service called MyService, with a contract interface IMyService. Given this, you’d host your service by using the ServiceHost class:

static void Main(string[] args)
using(ServiceHost theHost = new ServiceHost(typeof(MyService)))
Console.WriteLine(“Service opened, waiting for clients!”);

By placing your service hosting code in a using directive, you make sure that whenever the service stops, an exception is thrown, earthquake, volcano erupt, etc. happens, the Close method and by it, cleanup will be called. The code above assumes that you’ve added at least one endpoint by configuration (the good way) to your service, if you haven’t opening the service will throw an exception.

The other way of hosting is to use a Windows Service. I think have written a post about Windows Services in the exam-preparation set 70-536, so I won’t bother with the details of them. They are UIless, long-running processes in the background, which make your computer work. Read my post for more info. You’d use the overridden OnStart method of your Windows Service to do the same as you did in the entry point of the previous Console Application. I’m sure you won’t get stuck with it.

Now the third method, the hosting within the WCF-provided host (beautiful repetition of words). For this, you’ll need the help of WcfSvcHost.exe. This is a command-line tool, with very-very few possible arguments, which makes it one of my favorites from its kind.  It has the following signature:

WcfSvcHost.exe /service:pathToServiceDll /config:pathToServiceConfig

It has other options as well, but we’ll discuss them when we’d like to consume a service as a client. I think it isn’t hard to guess what WcfSvcHost.exe wants: a path for the service, compiled as a DLL, and a path for the configuration file of it. Then it’ll do its best to host the service. You should note that this form of hosting  is desired only during testing and studying, or when you are in the need of a rapid and inflexible solution.

Tags: , ,

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: