Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with hosting WCF service in a Windows Service

I have a WCF service that is hosted inside a Windows Service. The Windows Service has a OnStart method like this:

protected override void OnStart(string[] args)
{
  serviceHost = new ServiceHost(typeof (RouterService));
  serviceHost.Open();
}

It's quite minimal now, since I'm trying to find the problem. Since I have not put any error handling here, any exceptions at this point should have stopped the service from starting up properly. It would start, and then automatically stop again immediately.

My config file for the WCF service looks like this:

<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <system.serviceModel>
    <services>
      <service name="WcfService.RouterService"
               behaviorConfiguration="serviceBehavior" >
        <endpoint address="RouterService" contract="WcfService.IRouterService" binding="wsHttpBinding" bindingConfiguration="NoSecurity" />
        <endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="NoSecurity">
          <security mode="None" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior" >
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

In addition, I have added the NETWORK SERVICE user (who the service is running as to be able to listen to Http at the specified port:

netsh http add urlacl url=http://+:8000/ user="NETWORK SERVICE"

Now, in the client, I choose Add Service Reference. I can find the server, and VS also manage to create the configuration and make the proxy files. Then inside the client, I make an instance of the proxy, and open it. Everything still fine. But upon calling the first method on proxy, I get the following exception:

The HTTP service located at http://localhost:8000/RouterService is too busy.

I know that my client is the only thing that is trying to connect to this service, so why do I get this error message then? And how can I fix this? It does not time out when it tries to call the method, it throws the exception almost immediately.

EDIT

Ok, now I found out that my TestServer (not the Windows Client one, but a console application) also gives the same error. Earlier today it didn't, and neither the WCF service or TestServer are changed.

Then I tried changing the port to 8080 instead of 8000, and it worked. Then I tried the same for the Windows Service, and that also worked (after running the netsh http command on the new port)

So something has happened to http on port 8000 for some reason. I have tried restarting the system also of course.

This is puzzling me, so if anyone has any idea what is going on here I would appreciate it.

like image 518
Øyvind Bråthen Avatar asked May 20 '11 10:05

Øyvind Bråthen


People also ask

What is the need for activation or hosting of WCF service?

WAS Hosting − Hosting a WCF service in Windows Activation Service (WAS) is most advantageous because of its features such as process recycling, idle time management, common configuration system, and support for HTTP, TCP, etc.

What is difference between Windows service and WCF service?

A windows service is what you need. WCF is a communications library, and unless you plan to communicate with your application via a client, you don't need it. Your problem is related to activation, and keeping your code active in the background is what windows services do.

How do I host a Windows service?

Open Developer Command Prompt for Visual Studio and navigate to the project directory. Type installutil bin\service.exe at the command prompt to install the Windows service. Type services. msc at the command prompt to access the Service Control Manager (SCM).


1 Answers

Ok, after a long time of trying to figure out what happened here, I found the solution.

After I created the windows service, and tried to start it I couldn't because I had to register the NETWORK SERVICE user to listen on http post 8000 first. I therefore ran this command:

netsh http add urlacl url=http://+:8000/RouterService user="NETWORK SERVICE"

It gave a successful result, but then I tried starting the service, and got the same error message. Then I ran the same command without RouterService specified:

netsh http add urlacl url=http://+:8000/ user="NETWORK SERVICE"

It gave a successful result, and now my windows service started up without any problems, but I got the problems mentioned in the question above.

It seems that the first call to netsh causes all of these problems. I tried removing it again with this command:

netsh http delete urlacl url=http://+:8000/RouterService

And then everything worked perfectly.

A bitch to debug this since the exception had nothing to do with the actual problem, so I hope this question can save someone else a couple of hours :)

like image 71
Øyvind Bråthen Avatar answered Sep 20 '22 16:09

Øyvind Bråthen