0

UPDATE: The following was the case running on Windows Server 2016. Recently tried running on Windows Server 2022 where I can run the library as a console app successfully. It still won't run as a Windows Service (also tried running Windows Service with Administrator privileges.)

I have a .Net 4.8 class library that uses the Marfusios Websocket-client for communicating with a secure websocket (wss) server. When running as a Windows Service or as a console application, the client times out during the initial send request after the connect. However, if the client is run within a .Net Web Application hosted by IIS 10 on the same server as the windows service that fails, the client connects and communicates correctly.

When running under a Windows Service or console app, the library can connect/start the websocket client, but hangs on the Send to the server, which is triggered by the OnReconnection event for the websocket client. Using Fiddler with a proxy, I can see that the KeepAlive Ping-Pong messaging takes place after connection, but do not see the Send message that is sent in OnReconnection.

Eventually, an error from websocket-client bubbles up and reports the following error:

System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake. at System.Net.WebSockets.WebSocketBase.WebSocketOperation.d__19.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.WebSockets.WebSocketBase.d__45.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Websocket.Client.WebsocketClient.d__70.MoveNext()

I believe the error is due to the websocket server disconnecting after no communication from my client. The Windows Server running the websocket client library has a valid Starfield SSL certificate installed and has TLS1.2 installed. The websocket server owner says that they are using TLS1.2, confirmed using WireShark.

I have read and tried everything I can find related to this error and subject including:ValidateServerCertificate and System.Net.ServicePointManager.SecurityProtocol

Any help would be appreciated!

gmerkel
  • 1
  • 1

1 Answers1

0

The problem was an assembly dependency conflict. For some reason that I don't understand when running within a Windows Service, my library didn't load the local version of the System.Runtime.CompilerServices.Unsafe assembly required by the Marfusios Websocket-client until I added the following binding to the app.config file. This entry was not necessary when running my library from a console app or under IIS.

<dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>

The associated File Not Found exception did not bubble up from the Marfusios Websocket-client to my error log or to the Windows Application or System Event logs. It just failed silently because the error was being logged at a TRACE level and my solution's logging (Log4Net) was not capturing it. Including the Websocket-client project in my solution instead of referencing a NuGet package allowed me to modify the logging which revealed the problem.

gmerkel
  • 1
  • 1