ASP.NET 5 (DNX) 中托管在 IIS 上的 WebSockets 无法正常工作

WebSockets in ASP.NET 5 (DNX) hosted on IIS not working

我的网站使用 ASP.NET 5 和 RC1-Update1,与客户端的一些通信通过网络套接字(使用 SignalR)。

在测试期间,我发现虽然在 IIS(或 IIS Express)上 运行,但无法通过套接字进行通信。 SignalR 回退到长轮询。

SignalR 日志:

SignalR: Client subscribed to hub 'chathub'.
SignalR: Negotiating with '/signalr/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D'.
SignalR: serverSentEvents transport starting.
SignalR: Attempting to connect to SSE endpoint 'http://localhost:31650/signalr/signalr/connect?transport=serverSentEvents&c…wVlnXCY8bI7R8E&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&tid=2'.
SignalR: serverSentEvents transport timed out when trying to connect.
SignalR: EventSource calling close().
SignalR: serverSentEvents transport failed to connect. Attempting to fall back.
SignalR: foreverFrame transport starting.
SignalR: Forever Frame is not supported by SignalR on browsers with SSE support.
SignalR: foreverFrame transport failed to connect. Attempting to fall back.
SignalR: longPolling transport starting.
SignalR: Opening long polling request to 'http://localhost:31650/signalr/signalr/connect?transport=longPolling&client…8WRrhWwVlnXCY8bI7R8E&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D'.
SignalR: Long poll complete

经过长时间的调试,我通过命令行直接通过 dnx(没有 IIS 或其他任何东西)启动了我的网站:

dnx web

Web 套接字工作开箱即用,没有任何问题。

在我对解决方案的研究过程中,我发现在带有 socket.io 网络应用程序的 nodejs 中,必须禁用 IIS 中的网络套接字,以便它们正确转发到 nodejs(link 到资源 here on iis.net), 所以我尝试了同样的事情并得到了错误(一旦 web.config 中存在 webSocket 元素就会发生):

An error occurred attempting to determine the process id of the DNX process hosting your application.

我的web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
        </handlers>
        <httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" forwardWindowsAuthToken="false" stdoutLogEnabled="true" startupTimeLimit="3600" />
        <staticContent>
            <!-- A bunch of mime types (no difference if removed) -->
        </staticContent>
        <webSocket enabled="false" />
    </system.webServer>
</configuration>

此时我运行没主意了。我确保我在 windows 功能中安装了 WebSockets,并尝试在 Azure 上部署我的网站(启用 Web 套接字),完成后将在 Azure 上托管。

我会说问题是网络套接字没有被转发到 DNX,但找不到解决方案。任何帮助将不胜感激。

更新

Solution where the problem is recreated.

问题在于缺少包 Microsoft.AspNet.WebSockets.Server 并调用 startup.cs app.UseWebSockets()

问题已于 GitHub 解决,非常感谢@davidfowl。