WCF Client Throws WebSocketException when the Service Abruptly Closes the Handshake
- Dominant language
- C#
- Stars
- 1.8k
- Forks
- 576
- Avg merge
- 6d 9h
- Merged PRs (30d)
- 2
Description
**Describe the bug**
When WCF service unexpectedly closes a WebSocket connection, WCF client throws WebSocketException.
System.Net.WebSockets.WebSocketException : The remote party closed the WebSocket connection without completing the close handshake.
**To Reproduce**
Steps to reproduce the behavior:
1. Start SelfHostedCoreWCF service
2. Run any WebSocket test on Linux such as WebSocket_Http_RequestReply_Streamed
3. Close the connection from service side.
The client throws System.Net.WebSockets.WebSocketException.
**Expected behavior**
When the service unexpectedly closes a WebSocket connection, the client is expected to throw CommunicationException instead of WebSocketException.
To workaround the bug, we provide a temporary fix and catch WebSocketException while closing the channel factory objects. See PR: https://github.com/dotnet/wcf/pull/5802
```C#
catch (System.Net.WebSockets.WebSocketException)
{
// WCF Client has a bug which throws WebSocketException when the server closes the connection.
// The remote party closed the WebSocket connection without completing the close handshake.
comObj.Abort();
}
```
After this issue is resolved, we need to revert the code changes (remove the above code from [ScenarioTestHelpers.cs](https://github.com/dotnet/wcf/pull/5802/files#diff-46ddecf4b8b56bdbb995bc2e1618b66320b936938d0d34944778c07cf1329ceb)) and do not catch WebSocketException when closing the channel.
**Stack log**
WebSocketTests.WebSocket_Http_RequestReply_Streamed(messageEncoding: Text) [FAIL]
System.Net.WebSockets.WebSocketException : The remote party closed the WebSocket connection without completing the close handshake.
Stack Trace:
at System.ServiceModel.Channels.WebSocketTransportDuplexSessionChannel.ThrowOnPendingException(Exception& pendingException)
at System.ServiceModel.Channels.WebSocketTransportDuplexSessionChannel.WebSocketMessageSource.ReceiveAsync(TimeSpan timeout)
at System.ServiceModel.Channels.SynchronizedMessageSource.ReceiveAsync(TimeSpan timeout)
at System.ServiceModel.Channels.TransportDuplexSessionChannel.EnsureInputClosedAsync(TimeSpan timeout)
at System.ServiceModel.Channels.TransportDuplexSessionChannel.OnCloseAsync(TimeSpan timeout)
at System.ServiceModel.Channels.WebSocketTransportDuplexSessionChannel.OnCloseAsync(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.OnCloseAsyncInternal(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.System.ServiceModel.IAsyncCommunicationObject.CloseAsync(TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.OnCloseAsync(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.OnCloseAsyncInternal(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.System.ServiceModel.IAsyncCommunicationObject.CloseAsync(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.CloseAsyncInternal(TimeSpan timeout)
at System.Runtime.TaskHelpers.WaitForCompletion(Task task)
at System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Close()
at System.ServiceModel.Channels.ServiceChannelProxy.System.ServiceModel.ICommunicationObject.Close()
/_/src/System.Private.ServiceModel/tests/Common/Scenarios/ScenarioTestHelpers.cs(197,0): at ScenarioTestHelpers.CloseCommunicationObjects(ICommunicationObject[] objects)
/_/src/System.Private.ServiceModel/tests/Scenarios/Extensibility/WebSockets/WebSocketTests.4.1.0.cs(484,0):
Contributor guide
Assessment
This issue has not been assessed yet.