0
votes

I am trying to create a WCF application that will be hosted in a Windows Service

I have two options in Visual Web Developer

1) New Website -> WCF Service

2) New Project -> WCF Service Application

Which will be suitable for creating service that can be hosted in windows service? Is there any tutorial explaining hosting in windows service using any of the above mentioned methods?

Thanks

Lijo

3

3 Answers

1
votes

In most cases what you'll actually want to do is create two projects; first a WCF Service Library (that's under the WCF templates) and then a Windows Service (which is under the Windows templates), then reference the WCF Service Library from your Windows Service.

The WCF Service Library holds all of your WCF-specific classes - contracts, services, etc. - then you just create a ServiceHost inside the Windows service. This last part requires very little code:

public class MyService : ServiceBase // Windows Service class
{
    private ServiceHost host;

    protected override void OnStart(string[] args)
    {
        host = new ServiceHost(typeof(MyWcfLibrary.MyWcfService));
        host.Open();
    }

    protected override void OnStop()
    {
        host.Close();
    }
}

There's a more detailed tutorial here.

1
votes

As for creating your service - if you already plan to use a NT Service as your host, just use a class library and then add a WCF service to that. Neither of the two options given will give you something suitable for using in a NT Service.

For hosting, see:

0
votes

Lijo, This page in the MSDN help shows you how to create the WCF service by starting with a Console application. I was able to get everything done with this as a starting point.