I am having a problem that can be replicated as follows (you need IIS8, so it should be on Windows 8+ or Windows Server 2012 R2 +):
Create a new website in IIS Manager, say TestWs on port 8881, point to the new folder, say C: \ temp \ testws, and add the following Web.config file there.
<?xml version="1.0"?> <configuration> <system.web> <compilation targetFramework="4.5"/> <httpRuntime targetFramework="4.5"/> </system.web> </configuration>
Now add the following WsHandler.ashx file to the same folder
<%@ WebHandler Language="C#" Class="WsHandler" %> using System; using System.Threading; using System.Web; public class WsHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.AcceptWebSocketRequest(async webSocketContext => { while (true) { await webSocketContext.WebSocket.ReceiveAsync(new ArraySegment<byte>(new byte[1024]), CancellationToken.None); } }); } public bool IsReusable { get { return true; } } }
Then create a website from the developer toolbar in your browser, for example:
var ws = new WebSocket("ws://localhost:8881/wshandler.ashx"); ws.onclose = function() { console.log('closed'); };
In the task manager, you will see that for this application there is a w3wp.exe process, if you kill it, the client will receive the onclose event, and closed text will be printed.
However, if you create the website as described above and go to IIS Manager and restart the application pool, the website will not be closed, and now there will be two w3wp.exe processes.
Closing the web socket ws.close(); or updating the browser will disable the original w3wp.exe process.
It seems that the presence of an open websocket means that IIS will not be able to properly utilize the application pool.
Can someone figure out what to change in my code or what to change in IIS to make this work?