It's raising a single event for failed connects, and actual networking-related failures for failed connects will already be logged by the underlying API used.
By getting rid of the catch and rethrow, we can also avoid an extra layer of async state machine on connect.
<ItemGroup>
<Compile Include="System\Net\WebSockets\ClientWebSocket.cs" />
<Compile Include="System\Net\WebSockets\ClientWebSocketOptions.cs" Condition="'$(TargetsBrowser)' != 'true'" />
- <Compile Include="System\Net\WebSockets\NetEventSource.WebSockets.cs" />
<Compile Include="$(CommonPath)System\Net\UriScheme.cs" Link="Common\System\Net\UriScheme.cs" />
<Compile Include="$(CommonPath)System\Net\WebSockets\WebSocketValidate.cs" Link="Common\System\Net\WebSockets\WebSocketValidate.cs" />
- <Compile Include="$(CommonPath)System\Net\Logging\NetEventSource.Common.cs" Link="Common\System\Net\Logging\NetEventSource.Common.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetsBrowser)' != 'true'">
<Compile Include="System\Net\WebSockets\WebSocketHandle.Managed.cs" />
return ConnectAsyncCore(uri, cancellationToken);
}
- private async Task ConnectAsyncCore(Uri uri, CancellationToken cancellationToken)
+ private Task ConnectAsyncCore(Uri uri, CancellationToken cancellationToken)
{
_innerWebSocket = new WebSocketHandle();
- try
- {
- // Change internal state to 'connected' to enable the other methods
- if ((InternalState)Interlocked.CompareExchange(ref _state, (int)InternalState.Connected, (int)InternalState.Connecting) != InternalState.Connecting)
- {
- throw new ObjectDisposedException(GetType().FullName); // Aborted/Disposed during connect.
- }
- await _innerWebSocket.ConnectAsync(uri, cancellationToken, Options).ConfigureAwait(false);
- }
- catch (Exception ex)
+ // Change internal state to 'connected' to enable the other methods
+ if ((InternalState)Interlocked.CompareExchange(ref _state, (int)InternalState.Connected, (int)InternalState.Connecting) != InternalState.Connecting)
{
- if (NetEventSource.IsEnabled) NetEventSource.Error(this, ex);
- throw;
+ return Task.FromException(new ObjectDisposedException(nameof(ClientWebSocket))); // Aborted/Disposed during connect.
}
+
+ return _innerWebSocket.ConnectAsync(uri, cancellationToken, Options);
}
public override Task SendAsync(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken) =>
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using System.Diagnostics.Tracing;
-
-namespace System.Net
-{
- [EventSource(Name = "Microsoft-System-Net-WebSockets-Client")]
- internal sealed partial class NetEventSource { }
-}
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using System.Diagnostics.Tracing;
-using System.Reflection;
-using Xunit;
-
-namespace System.Net.WebSockets.Tests
-{
- public class LoggingTest
- {
- [Fact]
- public void EventSource_ExistsWithCorrectId()
- {
- Type esType = typeof(ClientWebSocket).GetTypeInfo().Assembly.GetType("System.Net.NetEventSource", throwOnError: true, ignoreCase: false);
- Assert.NotNull(esType);
-
- Assert.Equal("Microsoft-System-Net-WebSockets-Client", EventSource.GetName(esType));
- Assert.Equal(Guid.Parse("71cddde3-cf58-52d5-094f-927828a09337"), EventSource.GetGuid(esType));
-
- Assert.NotEmpty(EventSource.GenerateManifest(esType, "assemblyPathToIncludeInManifest"));
- }
- }
-}
<Compile Include="ClientWebSocketUnitTest.cs" />
<Compile Include="CloseTest.cs" />
<Compile Include="ConnectTest.cs" />
- <Compile Include="LoggingTest.cs" />
<Compile Include="KeepAliveTest.cs" />
<Compile Include="LoopbackHelper.cs" />
<Compile Include="ResourceHelper.cs" />