How do I set the ClientWebSocketOptions on a ClientWebSocket in the System.Net.WebSockets

Viewed 2673

I want to specify an ssl certificate for a web socket client to use when connecting to a web socket server.

The only constructor for a System.Net.WebSockets.ClientWebSocket has 0 parameters. I would expect there to be an override with (Options) as a parameter but there is not.

The ConnectAsync method has 2 parameters: Uri and CancellationToken. I would expect there to be an override with (Uri, CancellationToken, Options) but there is not.

The ClientWebSocket Options property is get but not set, so i cannot call

ws.options = MyClientWebSocketOptions

There is no SetOptions method on the ClientWebSocket class.

The ClientWebSocketOptions class has a ClientCertificates property that is is set-able, so I have assumed this is the way to specify the certificate.

I can create a ClientWebSocketOptions with the correct certificate, but i need to know how to make a ClientWebSocket use those options.

1 Answers

Sure, you can't do this: ws.options = MyClientWebSocketOptions, but you can do this:

var webSocket = new ClientWebSocket();
webSocket.Options.ClientCertificates.Add(new 
    System.Security.Cryptography.X509Certificates.X509Certificate());

It may be a getter, but that's fine as you can just modify the ClientWebSocketOptions the getter returns that the implementation instantiates for you.

Once you call ConnectAsync, that Options property becomes "read only" and you can no longer modify it (from what I remember)

Related