【问题标题】:Missing link between ServiceHost and IISServiceHost 和 IIS 之间缺少链接
【发布时间】:2017-07-18 01:30:38
【问题描述】:

当我设置多个 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) 正在运行覆盖https://localhost 的 IIS。没有托管站点,IIS 基本上什么也没做。 当我在地址 https://localhost/myservice.svc 上安装使用 WCF ServiceHost 的程序(普通 Windows 服务)时,它可以正常工作。

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

【问题讨论】:

    标签: wcf iis


    【解决方案1】:

    监听 http 的不是 IIS,而是 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:
    

    正如您在上面的输出中看到的,查看 Registered URLS,特定进程可以在其中侦听 URL 的一部分。所以在这种情况下 HTTP://:80/BUGGYBITS/ 将进入一个特定的进程,而 HTTP://:80/ 将进入另一个进程。

    希望这一切都清楚。您可以找到有关 netsh 命令here 的更多详细信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多