Search code examples
c#.netwcfwebsocketservicehost

WCF ServiceHost configuration for use with the WebSocket Secure over TLS


I have created a WebSocket server using ServiceHost:

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();
}

It works fine with my JavaScript application. Currently I am trying to add support for WebSocket Secure. I have changed the HttpTransportBindingElement to HttpsTransportBindingElement, the baseurl to https:// and connection url in JavaScript application to 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();
}

I was expecting it will work, application started without errors, but after connecting in JavaScript I am getting this error.

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

Are there any other settings of ServiceHost necessary to use WebSocket secure? Is WebSocket secure implemented in Microsoft's ServiceModel?


Solution

  • Finally I have found the solution. The code above mentioned works for the scenario, but I had to configure a certificate for the host machine and run client application over HTTPS. This post was helpful Self hosted WCF using WebSockets is not working using SSL