views:

669

answers:

3

I am having some trouble hosting a WCF service inside a Windows Service. I can start my WCF service in VS2008 and by navigating to the base address in my app.config

<configuration>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="WCF.IndexerBehavior"
        name="WCF.Indexer">
        <endpoint address="" binding="wsHttpBinding" contract="WCF.IIndexer">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" 
contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/WCFService/Action/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WCF.IndexerBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

I can see it works fine, I get the page saying I created a service and code samples on how to use it are shown.

Now my next step was to create a Windows Service to host my WCF shown above.

I just used te windows service template, it gave me a Program.cs and Service1.cs which I renamed to WindowsServiceHost.cs. In it I have:

private ServiceHost host;

        public WindowsServiceHost()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                var serviceType = typeof(Indexer.WCF.Indexer);
                host = new ServiceHost(serviceType);
                host.Open();
            }
            catch (Exception ex)
            {

            }
        }

        protected override void OnStop()
        {
            if (host != null)
            {
                host.Close();
            }
        }

Everything compiles fine, I can run InstallUtil (I defined an installer). The service used to start and stop immediately but disabling Windows Defender got rid of this. Now the service starts (As a network service) and stays up (I think), but when I navigate to the base address, I get the not found page. Another weird thing is when I try to stop the service (which is still displayed as running) I get:

Error 1061: The service cannot accept control messages at this time

I've tried everything but am at a loss.

+1  A: 

Not 100% sure what the reason really is - just to confirm, we self-host WCF services in Windows services all the time and it generally works perfectly fine.

Two points you could try - just to get a feeling for the behavior and a potential clue for the problem:

1) I notice you open the ServiceHost with just the type of the service - that works, but you might still want to add a base address even to the call of the new ServiceHost() - like this:

host = new ServiceHost(serviceType, 
                       new Uri("http://localhost:8181/WCFService/Action/");

Can you navigate to that address and get the service page??

2) The other thing I noticed is that your service seems to be called Indexer.WCF.Indexer as specified in the typeof() before opening the host, but in the config file, the name= on the <service> tag is only "WCF.Indexer".

Could you possibly try to change that tag to read:

<service behaviorConfiguration="WCF.IndexerBehavior"
         name="Indexer.WCF.Indexer">

Does that help? Are you now able to see the service page when navigating to it in the browser?

Marc

marc_s
A: 

Try removing the catch statement, there may be an error that you are not seeing

Shiraz Bhaiji
A: 

Self-hosting HTTP in a Windows service may require registering the endpoint with HttpCfg.exe. Take a look here.

Steven Sudit