开发者

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开发者_如何学C 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.


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


Following answer of @oyvind-knobloch-brathen consider to use well known SID strings instead of "user" command line key, this will give you cross culture portability (I experienced problems on French-localized Win7 machine). For more information see SID strings. For your case this will be:

netsh http add urlacl url=http://+:8000/ sddl="O:NS"

a link to SDDL on MSDN.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜