tags:

views:

57

answers:

3

Hi Team,

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

+1  A: 

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.

Aaronaught
Thanks. But the problem is that I don't have a WCF temaplate section in my "Visual Studio installed templates" in Visual Web Developer. Is it something to be separately installed? Is there a way to convert a simple class library into WCF Service Librarby using some configurations?
Lijo
@Lijo: I missed the Visual Web Developer part, you sure you don't see that under the "Visual C#" tree? If not, then you don't need the template, you just need to add a few assembly references (mainly `System.ServiceModel` and `System.Runtime.Serialization`).
Aaronaught
+1  A: 

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:

marc_s
A: 

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.

Randy Eppinger