WCF works as an application, but not as a service

I have a WCF server that I can run as a service or as an application for Windows forms. When I run it as a Windows Forms application, I can connect to it through my client application. However, when I start it as a service using the same code, I cannot connect to it. I confirmed that the service is up and running. The following is the server configuration 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 calling OnStart:

 if (this.serviceHost != null) { this.serviceHost.Close(); } this.serviceHost = new ServiceHost(typeof(ManagementService)); this.serviceHost.Open(); 

and client configuration 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> 
+4
source share
6 answers

Could you post the rest of your code to host the service?

Your class that starts the service must inherit from "ServiceBase" and must implement the OnStart and OnStop methods. These methods are called by the service console to start and stop the service process, so your ServiceHost must be open / close in these methods. Just wondering, maybe you are not doing this.

+2
source

What account does the service work in? I wonder if the service is starting, possibly due to the lack of permissions to open the port.

Try to start the service in your own personality (but as a service). If it works, this is a permission issue. These are most likely HTTP.SYS permissions.

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

+1
source

Where did you get the code from which the service host is created? My suggestion is that when you start it as a service, you either do not create a ServiceHost or do not save a link to it (so that it collects garbage)

0
source

If you are on the same machine, I would suggest using NetNamedPipeBinding instead of WSHttpBinding. It's faster. You can always return to ws-http if you need to cross-use the machine along the way.

Make sure your service is actually running through the TaskManager. If not, put the Debugger.Break () statement in your service constructor and go through to find where it does not start. Here's a quick step by step to create a Windows NT service in C # (if you need it).

0
source

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

Have you tried to debug the service (using visual studio for the process)?

0
source

Have you verified that you have such a configuration defined in the configuration files for both the WinForms application and the service?

0
source

All Articles