WCF ServiceHost配置与WebSocket Secure over TLS一起使用

时间:2016-03-10 12:20:40

标签: c# .net wcf websocket servicehost

我使用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更改为Http s TransportBindingElement,将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中实现安全?

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案。上面提到的代码适用于该场景,但我必须为主机配置证书并通过HTTPS运行客户端应用程序。这篇文章很有用Self hosted WCF using WebSockets is not working using SSL

相关问题