Hosting WCF Service as Windows Service

85 views Asked by At

I have created WCF service project. It has following content in SVC file.

    <%@ ServiceHost Service="Deepak.BusinessServices.Implementation.ApiImplementation"
      Factory="Deepak.BusinessServices.Implementation.CustomServiceHostFactory"%>

SVC reference

    http://localhost/DeepakGateway/Service.svc

Service is UP and WSDL generated. Now I want to host this service as Windows Service. How can I do it?

I have created "Windows Service" Project ans have following code.

protected override void OnStart(string[] args)
    {
        if (m_Host != null)
        {
            m_Host.Close();
        }
        Uri httpUrl = new Uri("http://localhost/DeepakGateway/Service.svc");

        m_Host = new ServiceHost
        (typeof(?????? WHAT TO FILL HERE?), httpUrl);
        //Add a service endpoint
        m_Host.AddServiceEndpoint
        (typeof(?????? WHAT TO FILL HERE?), ), new WSHttpBinding(), "");
        //Enable metadata exchange
        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        m_Host.Description.Behaviors.Add(smb);
        //Start the Service
        m_Host.Open();


    }
1

There are 1 answers

1
marc_s On

You need to add the type of the class that implements your service contract in the ServiceHost constructor, and type of the service contract in your AddServiceEndpoint

Assuming your service implementation class looks something like this:

namespace Deepak.BusinessServices.Implementation
{ 
    public class ApiImplementation : IApiImplementation
    {
       ....
    }
 }

then you need:

m_Host = new ServiceHost(typeof(ApiImplementation), httpUrl);
m_Host.AddServiceEndpoint(typeof(IApiImplementation), new WSHttpBinding(), "");
  • the service host needs to know what (concrete) type of service class to host
  • the endpoint needs to know what service contract (interface) it exposes