When I setup multiple ServiceHost instances I can only use one host for one port.
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
The second call to host2.Open() fails as expected.
Recently I found a stange behaviour of ServiceHost by coincidence. A machine (Windows Server 2012 R2) was running IIS covering https://localhost . There are no sites hosted, IIS basically did nothing. When I installed my program (a normal windows service) that uses WCF ServiceHost on address https://localhost/myservice.svc it worked without any problem.
I did neither configure IIS nor did I "tell" ServiceHost that there is a running IIS. How is it possible that there aren't any port conflicts? Which dark magic does ServiceHost use? Just want to understand what's going on.
It is not IIS which is listening on http but http.sys driver(HTTP Service) is doing that on Windows OS. And this magic is done in http.sys level.You can also do this and see what application is listening on what .IIS(w3wp.exe process) or your windows service (which is using HTTP listener) recieves the request from the queue provided by HTTP.sys.
To see this in action,you can run the netsh command
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:
As you see in the above output,look at the Registered URLS where a particular process can listen for a part of the URL. so in this case HTTP:// :80/BUGGYBITS/ will go a particular process and HTTP:// :80/ will go to another.
Hope this clears.You can find more details about netsh command here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.