2017-07-17 58 views
0

当我设置多个ServiceHost实例时,我只能为一个端口使用一台主机。缺少ServiceHost和IIS之间的链接

Uri baseAddressHttps = new Uri("https://localhost/myservice.svc"); 
ServiceHost host = new ServiceHost(typeof(MyService), baseAddressHttps); 
... 
host.Open(); //OK 

ServiceHost host2 = new ServiceHost(typeof(MyService), baseAddressHttps); 
... 
host2.Open(); //Fail 

第二次调用host2.Open()会按预期失败。

最近我偶然发现了ServiceHost的行为行为。 一台机器(Windows Server 2012 R2)运行的IIS覆盖了https://localhost。没有托管的站点,IIS基本上什么也没做。 当我安装了我的程序(一个正常的Windows服务),使用地址https://localhost/myservice.svc上的WCF ServiceHost它没有任何问题。

我没有配置IIS,也没有“告诉”ServiceHost有一个正在运行的IIS。怎么可能没有任何港口冲突? ServiceHost使用哪种黑暗魔法?只是想了解发生了什么。

回答

0

这不是IIS正在HTTP上侦听,而是http.sys驱动程序(HTTP 服务)正在Windows操作系统上这样做。这个神奇的功能是在 http.sys级别完成的。你也可以这样做,看看什么应用程序是 监听什么.IIS(w3wp.exe进程)或者你的windows服务(它使用HTTP监听器)接收来自队列由HTTP.sys提供。

要看到这个动作,当你在上面的输出看你可以运行netsh命令

C:\windows\system32>netsh http show servicestate 

Snapshot of HTTP service state (Server Session View): 
----------------------------------------------------- 

Server session ID: FF00000020000001 
    Version: 2.0 
    State: Active 
    Properties: 
     Max bandwidth: 4294967295 
     Timeouts: 
      Entity body timeout (secs): 120 
      Drain entity body timeout (secs): 120 
      Request queue timeout (secs): 65535 
      Idle connection timeout (secs): 120 
      Header wait timeout (secs): 120 
      Minimum send rate (bytes/sec): 240 
    URL groups: 
    URL group ID: FE00000040000001 
     State: Active 
     Request queue name: DefaultAppPool 
     Properties: 
      Max bandwidth: inherited 
      Max connections: 4294967295 
      Timeouts: 
       Entity body timeout (secs): 120 
       Drain entity body timeout (secs): 120 
       Request queue timeout (secs): 65535 
       Idle connection timeout (secs): 120 
       Header wait timeout (secs): 0 
       Minimum send rate (bytes/sec): 0 
      Logging information: 
       Log directory: C:\inetpub\logs\LogFiles\W3SVC1 
       Log format: 0 
      Authentication Configuration: 
       Authentication schemes enabled: 
    URL group ID: FD00000040000001 
     State: Active 
     Request queue name: testweb 
     Properties: 
      Max bandwidth: inherited 
      Max connections: inherited 
      Timeouts: 
       Timeout values inherited 
      Authentication Configuration: 
       Authentication schemes enabled: 
      Number of registered URLs: 1 
      Registered URLs: 
       HTTP://*:80/BUGGYBITS/ 
    URL group ID: FF00000240000001 
     State: Active 
     Request queue name: testweb 
     Properties: 
      Max bandwidth: inherited 
      Max connections: 4294967295 
      Timeouts: 
       Entity body timeout (secs): 120 
       Drain entity body timeout (secs): 120 
       Request queue timeout (secs): 65535 
       Idle connection timeout (secs): 120 
       Header wait timeout (secs): 0 
       Minimum send rate (bytes/sec): 0 
      Logging information: 
       Log directory: C:\inetpub\logs\LogFiles\W3SVC2 
       Log format: 0 
      Authentication Configuration: 
       Authentication schemes enabled: 
      Number of registered URLs: 1 
      Registered URLs: 
       HTTP://*:80/ 

Server session ID: FF00000120000001 
    Version: 2.0 
    State: Active 
    Properties: 
     Max bandwidth: 4294967295 
     Timeouts: 
      Entity body timeout (secs): 120 
      Drain entity body timeout (secs): 120 
      Request queue timeout (secs): 120 
      Idle connection timeout (secs): 120 
      Header wait timeout (secs): 120 
      Minimum send rate (bytes/sec): 150 
    URL groups: 
    URL group ID: FE00000140000001 
     State: Active 
     Request queue name: Request queue is unnamed. 
     Properties: 
      Max bandwidth: inherited 
      Max connections: inherited 
      Timeouts: 
       Timeout values inherited 
      Number of registered URLs: 1 
      Registered URLs: 
       HTTP://*:5357/B5B45532-3676-4316-BBB8-9DBB35BACAB4/ 

Request queues: 
    Request queue name: DefaultAppPool 
     Version: 2.0 
     State: Active 
     Request queue 503 verbosity level: Limited 
     Max requests: 1000 
     Number of active processes attached: 0 
     Controller process ID: 3016 
     Process IDs: 

    Request queue name: testweb 
     Version: 2.0 
     State: Active 
     Request queue 503 verbosity level: Limited 
     Max requests: 1000 
     Number of active processes attached: 0 
     Controller process ID: 3016 
     Process IDs: 

,看其中某一项工艺可以监听的一部分登录的URL网址。所以在这种情况下,HTTP:// :80/BUGGYBITS /将进入特定进程,并且HTTP://:80 /将转到另一个进程。

希望这个清除。你可以找到更多关于netsh命令的细节here