简体   繁体   中英

WCF works as application, but not as service

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>

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.

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.

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)

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

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

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

您是否检查过是否在 WinForms 应用程序和服务的配置文件中定义了该配置?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM