views:

345

answers:

6

I have a WCF server that I can run as a service or as a windows forms application. When I run it as a Windows Forms application I can connect to it via my client application. However when I run it as a service using the same code, I cannot connect to it. I have confirmed that the service is running and doing its work. Below is the server's config file.

<system.serviceModel>
  <services>
    <service name="Cns.TrafficCopService.ManagementService">
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8000/TrafficCop/ManagementService" />
        </baseAddresses>
      </host>
      <endpoint address="" binding="wsHttpBinding" contract="Cns.TrafficCopService.IManagementService" />
    </service>
  </services>
</system.serviceModel>

and its hosting code, called 100 milliseconds after OnStart is called:

if (this.serviceHost != null)
{
    this.serviceHost.Close();
}

this.serviceHost = new ServiceHost(typeof(ManagementService));
this.serviceHost.Open();

and the client's config file:

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="WSHttpBinding_IManagementService" />
    </wsHttpBinding>
  </bindings>
  <client>
    <endpoint
        address="http://localhost:8000/TrafficCop/ManagementService"
        binding="wsHttpBinding"
        bindingConfiguration="WSHttpBinding_IManagementService"
        contract="IManagementService"
        name="WSHttpBinding_IManagementService">
    </endpoint>
  </client>
</system.serviceModel>
A: 

Where did you take the code that creates the service host from? My fist guess would be that when you run it as a service you either don't create the ServiceHost, or you don't keep the reference to it (so it gets garbage-collected)

Grzenio
The field that holds the service is on the Service itself and it shouldn't be garbage collected.
Orion Adrian
+1  A: 

What account is the service running as? I wonder if the service is failing to start, probably due to not having permissions to open the port.

Try running the service in your own identity (but as a service). If it works, it is a permissions issue. The most likely is the HTTP.SYS permissions.

To assign access, you use netsh on vista/window 7, or httpcfg on xp.

Marc Gravell
Also - if it isn't on the same machine, you may need to change the address to be the actual name used by the client, rather than "localhost".
Marc Gravell
I was running as myself (a local admin with Run as a Service rights).
Orion Adrian
Well, worth a shot...
Marc Gravell
+2  A: 

Could you post the rest of your code for hosting the service?

Your class that starts the service should be inheriting from "ServiceBase" and should implement the "OnStart" and "OnStop" methods. These methods are invoked by the service console to start and stop the service process, so your ServiceHost should be opened/closed in these methods. Just wondering if maybe you're not doing that.

Andy White
I am in fact doing that.
Orion Adrian
It turns out the Timer class I was using (System.Threading.Timer and System.Forms.Timer wasn't working properly in the service). I have replaced them with System.Timers.Timer which is working correctly.
Orion Adrian
A: 

If you are on the same machine, I would suggest using the NetNamedPipeBinding instead of WSHttpBinding. It's faster. You can always change back to the ws-http if you need cross-machine usage down the road.

Make sure your service is actually running via TaskManager. If not, put a Debugger.Break() statement in your service's constructor and step through to find where it fails to start. Here is a concise step-by-step for creating a Windows NT service in C# (if you need it).

Matt Davis
The service is running and doing its work.
Orion Adrian
A: 

Nothing in the event log about failing to register the address?

did you try to debug the service (using visual studio attach to process)?

Philippe
A: 

Have you checked that you have that configuration defined in the config files for both the WinForms app and for the service?

David M