用于 WebSocket Secure over TLS 的 WCF ServiceHost 配置

WCF ServiceHost configuration for use with the WebSocket Secure over TLS

我已经使用 ServiceHost 创建了一个 WebSocket 服务器:

using (ServiceHost host = new ServiceHost(typeof(WebSocketsServer), baseAddress))
{
    CustomBinding binding = new CustomBinding();
    binding.Elements.Add(new ByteStreamMessageEncodingBindingElement());
    HttpTransportBindingElement transport = new HttpTransportBindingElement();
    transport.WebSocketSettings.TransportUsage = WebSocketTransportUsage.Always;
    transport.WebSocketSettings.CreateNotificationOnConnection = true;
    binding.Elements.Add(transport);

    host.AddServiceEndpoint(typeof(IWebSocketsServer), binding, "");

    host.Open();
}

我的 JavaScript 应用程序运行良好。目前我正在尝试添加对 WebSocket Secure 的支持。我已将 HttpTransportBindingElement 更改为 HttpsTransportBindingElement,将 baseurl 更改为 https:// 并将 JavaScript 应用程序中的连接 url 更改为 wss:/ /

using (ServiceHost host = new ServiceHost(typeof(WebSocketsServer), baseAddress))
{
    CustomBinding binding = new CustomBinding();
    binding.Elements.Add(new ByteStreamMessageEncodingBindingElement());
    HttpsTransportBindingElement transport = new HttpsTransportBindingElement();
    transport.WebSocketSettings.TransportUsage = WebSocketTransportUsage.Always;
    transport.WebSocketSettings.CreateNotificationOnConnection = true;

    binding.Elements.Add(transport);


    host.AddServiceEndpoint(typeof(IWebSocketsServer), binding, "");


    host.Open();
}

我原以为它会工作,应用程序启动时没有错误,但是在 JavaScript 连接后我收到了这个错误。

WebSocket connection to 'wss://localhost:5555/wsData' failed: Error in connection establishment: net::ERR_CONNECTION_RESET

为了安全地使用 WebSocket,是否需要对 ServiceHost 进行任何其他设置? WebSocket 是否在 Microsoft 的 ServiceModel 中安全实现?

终于找到解决办法了。上面提到的代码适用于该场景,但我必须通过 HTTPS 为主机和 运行 客户端应用程序配置证书。这个 post 很有帮助