tags:

views:

296

answers:

3

Hello there,

I have two Services called TemplateService, TemplateReportService (both defined in one WCF Service Library) to be exposed to the client application.

How can I host these two services under one Windows Service?

Please guide.

Thank you!

+2  A: 

Yes, sure, no problem - you just need to open two service hosts:

    protected override void OnStart(string[] args)
    {
        ServiceHost host1 = new ServiceHost(typeof(Service1));
        ServiceHost host2 = new ServiceHost(typeof(Service2));

        host1.Open();
        host2.Open();
    }

and of course you need to have the appropriate config entries for those two separate services in the Windows service's app.config file.

Marc

marc_s
A: 

In the app.config for your Windows service, define a unique endpoint for each WCF service. Then in the OnStart() method of your Windows service, create a ServiceHost instance for each WCF service class.

Matt Davis
A: 

Thank you Marc and Matt for your reply.

Here is snippet from my Windows Service App.config:

 <services>
  <service behaviorConfiguration="ReportingComponentLibrary.TemplateServiceBehavior"
    name="ReportingComponentLibrary.TemplateService">
    <endpoint address="ws" binding="wsHttpBinding" contract="ReportingComponentLibrary.ITemplateService" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" ></endpoint>
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8080/ReportingComponentLibrary/TemplateService" />
      </baseAddresses>
    </host>
  </service>
  <service behaviorConfiguration="ReportingComponentLibrary.TemplateServiceBehavior"
    name="ReportingComponentLibrary.TemplateReportService">
    <endpoint address="ws" binding="wsHttpBinding" contract="ReportingComponentLibrary.ITemplateReportService" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" ></endpoint>
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8080/ReportingComponentLibrary/TemplateReportService" />
      </baseAddresses>
    </host>
  </service>
</services>

And I have opened two hosts for these two services as specified by Marc.

But when I try to start my service, it says... 'service started and stopped...'. I looked into eventvwr for error detail and it says:

Service cannot be started. System.InvalidOperationException: Service 'ReportingComponentLibrary.TemplateReportService' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.

Any thoughts how I can avoid this error?

Thank you!

inutan
Put a call to System.Diagnostics.Debug.Break() inside your OnStart() method, compile your Windows service in debug mode, and start your service. You should be prompted to enter a debug session which will allow you to debug the service. Once inside the debug session, go to the Debug menu and select the Exceptions... menu option. In that dialog, select the checkbox under the Thrown column for CLR Exceptions. Hit the Go button, and the service will break when an exception is triggered, allowing you to see exactly what is causing the problem. You can diagnose from there.
Matt Davis
Can you show us your lines of code in OnStart()?
marc_s
Or another thing to check: the error also says that there might not be a config file at all. Can you check? Is there a config file in the directory where the Windows NT Service is installed?
marc_s
Thanks for your reply. The mistake I was doing was to copy app.config from WCF Library to Windows Service Installation folder. Whereas I just had to make changes in <ServiceName>.exe.config found in Windows Service Installation folder.
inutan