缺少ServiceHost和IIS之间的链接

时间:2017-07-17 13:40:17

标签: wcf iis

当我设置多个ServiceHost实例时,我只能为一个端口使用一个主机。

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基本上什么也没做。 当我安装在地址https://localhost/myservice.svc上使用WCF ServiceHost的程序(普通的Windows服务)时,它没有任何问题。

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

1 个答案:

答案 0 :(得分: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 ,其中特定进程可以侦听URL的一部分。所以在这种情况下,HTTP:// :80 / BUGGYBITS /将进入特定进程,HTTP:// :80 /将进入另一个进程。

希望这一点清除。您可以找到有关netsh命令here

的更多详细信息