5

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>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Orion Adrian
  • 19,053
  • 13
  • 51
  • 67
  • Where do you run the hosting code? – Grzenio Mar 25 '09 at 14:28
  • Are the client and server on the same machine? I wonder if "localhost" isn't part of the problem... a remote client isn't going to be asking for "localhost"... – Marc Gravell Mar 25 '09 at 14:44
  • 1
    They are on the same machine. Again it works as an application, but not as a service. – Orion Adrian Mar 25 '09 at 14:50
  • When you say you can't connect, what do you mean? I assume an error message on the client side? Let us know that message and it may help. – Daniel Mar 25 '09 at 21:26
  • Did you ever get to the bottom of this? I'm having the same issue, and have tried all the things suggested here, without success. :-( – DannyT Nov 01 '09 at 23:07

6 Answers6

2

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
  • 86,444
  • 48
  • 176
  • 211
  • 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 Mar 26 '09 at 14:43
1

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.

Community
  • 1
  • 1
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

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
  • 35,875
  • 47
  • 158
  • 240
0

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).

Community
  • 1
  • 1
Matt Davis
  • 45,297
  • 16
  • 93
  • 124
0

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
  • 3,945
  • 3
  • 38
  • 56
0

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

David M
  • 71,481
  • 13
  • 158
  • 186