Skip to content

Commit 9cda879

Browse files
geoffkizerGeoffrey Kizerscalablecory
committed
Remove System.Net.Connections and related features (dotnet#41648)
* remove System.Net.Connections and related features * cleanup csproj changes * Update src/libraries/pkg/baseline/packageIndex.json Co-authored-by: Cory Nelson <phrosty@gmail.com> * Update src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs Co-authored-by: Cory Nelson <phrosty@gmail.com> Co-authored-by: Geoffrey Kizer <geoffrek@windows.microsoft.com> Co-authored-by: Cory Nelson <phrosty@gmail.com>
1 parent 38017c3 commit 9cda879

32 files changed

+249
-420
lines changed

src/libraries/NetCoreAppLibrary.props

-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
System.IO.FileSystem.Watcher;
4949
System.IO.IsolatedStorage;
5050
System.IO.MemoryMappedFiles;
51-
System.IO.Pipelines;
5251
System.IO.Pipes;
5352
System.IO.Pipes.AccessControl;
5453
System.IO.UnmanagedMemoryStream;
@@ -57,7 +56,6 @@
5756
System.Linq.Parallel;
5857
System.Linq.Queryable;
5958
System.Memory;
60-
System.Net.Connections;
6159
System.Net.Http;
6260
System.Net.Http.Json;
6361
System.Net.HttpListener;

src/libraries/System.Net.Connections/ref/System.Net.Connections.cs

+21
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,24 @@ public SocketsConnectionFactory(System.Net.Sockets.SocketType socketType, System
7979
protected virtual System.Net.Sockets.Socket CreateSocket(System.Net.Sockets.AddressFamily addressFamily, System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType, System.Net.EndPoint? endPoint, System.Net.Connections.IConnectionProperties? options) { throw null; }
8080
}
8181
}
82+
namespace System.Net
83+
{
84+
public enum NetworkError : int
85+
{
86+
Other = 0,
87+
EndPointInUse,
88+
HostNotFound,
89+
TimedOut,
90+
ConnectionRefused,
91+
OperationAborted,
92+
ConnectionAborted,
93+
ConnectionReset,
94+
}
95+
public class NetworkException : System.IO.IOException
96+
{
97+
public NetworkException(NetworkError error, Exception? innerException = null) { }
98+
public NetworkException(string message, NetworkError error, Exception? innerException = null) { }
99+
protected NetworkException(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext) { }
100+
public NetworkError NetworkError { get { throw null; } }
101+
}
102+
}

src/libraries/System.Net.Connections/src/System.Net.Connections.csproj

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<ItemGroup>
77
<Compile Include="$(CommonPath)System\Net\NetworkErrorHelper.cs" Link="Common\System\Net\NetworkErrorHelper.cs" />
88
<Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs" Link="Common\System\Threading\Tasks\TaskToApm.cs" />
9+
<Compile Include="System\Net\NetworkError.cs" />
10+
<Compile Include="System\Net\NetworkException.cs" />
911
<Compile Include="System\Net\Connections\ConnectionBase.cs" />
1012
<Compile Include="System\Net\Connections\ConnectionCloseMethod.cs" />
1113
<Compile Include="System\Net\Connections\ConnectionExtensions.cs" />
@@ -20,13 +22,13 @@
2022
<Compile Include="System\Net\Connections\Sockets\SocketsConnectionFactory.cs" />
2123
</ItemGroup>
2224
<ItemGroup>
25+
<ProjectReference Include="..\..\System.IO.Pipelines\ref\System.IO.Pipelines.csproj" />
2326
<Reference Include="System.Runtime" />
2427
<Reference Include="System.Memory" />
2528
<Reference Include="System.Net.Primitives" />
2629
<Reference Include="System.Net.Sockets" />
2730
<Reference Include="System.Threading" />
2831
<Reference Include="System.Threading.Tasks" />
29-
<Reference Include="System.IO.Pipelines" />
3032
<Reference Include="Microsoft.Win32.Primitives" />
3133
</ItemGroup>
3234
</Project>

src/libraries/System.Net.Primitives/src/System/Net/NetworkException.cs src/libraries/System.Net.Connections/src/System/Net/NetworkException.cs

+2-11
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,7 @@ public override void GetObjectData(SerializationInfo serializationInfo, Streamin
3838
/// <summary>Returns the specific kind of error.</summary>
3939
public NetworkError NetworkError { get; }
4040

41-
private static string GetExceptionMessage(NetworkError error) => error switch
42-
{
43-
NetworkError.EndPointInUse => SR.networkerror_addressinuse,
44-
NetworkError.TimedOut => SR.networkerror_timedout,
45-
NetworkError.HostNotFound => SR.networkerror_hostnotfound,
46-
NetworkError.ConnectionRefused => SR.networkerror_connectionrefused,
47-
NetworkError.ConnectionAborted => SR.networkerror_connectionaborted,
48-
NetworkError.ConnectionReset => SR.networkerror_connectionreset,
49-
NetworkError.OperationAborted => SR.networkerror_operationaborted,
50-
_ => SR.networkerror_other
51-
};
41+
// TODO: Better exception messages
42+
private static string GetExceptionMessage(NetworkError error) => $"A network error occurred: {error}";
5243
}
5344
}

src/libraries/System.Net.Connections/tests/System.Net.Connections.Tests/Sockets/SocketsConnectionFactoryTests.cs

-30
Original file line numberDiff line numberDiff line change
@@ -225,36 +225,6 @@ public async Task Connection_Pipe_ReadWrite_Success(EndPoint endPoint, SocketTyp
225225
Assert.True(rr.Buffer.FirstSpan.SequenceEqual(sendData));
226226
}
227227

228-
[Fact]
229-
public async Task Connection_Stream_FailingOperation_ThowsNetworkException()
230-
{
231-
using var server = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, IPAddress.Loopback);
232-
using SocketsConnectionFactory factory = new SocketsConnectionFactory(SocketType.Stream, ProtocolType.Tcp);
233-
using Connection connection = await factory.ConnectAsync(server.EndPoint);
234-
235-
connection.ConnectionProperties.TryGet(out Socket socket);
236-
Stream stream = connection.Stream;
237-
socket.Dispose();
238-
239-
Assert.Throws<NetworkException>(() => stream.Read(new byte[1], 0, 1));
240-
Assert.Throws<NetworkException>(() => stream.Write(new byte[1], 0, 1));
241-
}
242-
243-
[Fact]
244-
public async Task Connection_Pipe_FailingOperation_ThowsNetworkException()
245-
{
246-
using var server = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, IPAddress.Loopback);
247-
using SocketsConnectionFactory factory = new SocketsConnectionFactory(SocketType.Stream, ProtocolType.Tcp);
248-
using Connection connection = await factory.ConnectAsync(server.EndPoint);
249-
250-
connection.ConnectionProperties.TryGet(out Socket socket);
251-
IDuplexPipe pipe = connection.Pipe;
252-
socket.Dispose();
253-
254-
await Assert.ThrowsAsync<NetworkException>(() => pipe.Input.ReadAsync().AsTask());
255-
await Assert.ThrowsAsync<NetworkException>(() => pipe.Output.WriteAsync(new byte[1]).AsTask());
256-
}
257-
258228
[Theory]
259229
[InlineData(false, false)]
260230
[InlineData(false, true)]

src/libraries/System.Net.Connections/tests/System.Net.Connections.Tests/System.Net.Connections.Tests.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,9 @@
3030
<Compile Include="$(CommonTestPath)System\Net\Sockets\SocketImplementationType.cs" Link="SocketCommon\SocketImplementationType.cs" />
3131
<Compile Include="$(CommonTestPath)System\Threading\Tasks\TaskTimeoutExtensions.cs" Link="Common\System\Threading\Tasks\TaskTimeoutExtensions.cs" />
3232
</ItemGroup>
33+
<ItemGroup>
34+
<ProjectReference Include="..\..\..\System.IO.Pipelines\src\System.IO.Pipelines.csproj" />
35+
<ProjectReference Include="..\..\..\System.Net.Connections\src\System.Net.Connections.csproj" />
36+
</ItemGroup>
3337

3438
</Project>

src/libraries/System.Net.Http/ref/System.Net.Http.cs

-2
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ public SocketsHttpHandler() { }
331331
public static bool IsSupported { get { throw null; } }
332332
public bool AllowAutoRedirect { get { throw null; } set { } }
333333
public System.Net.DecompressionMethods AutomaticDecompression { get { throw null; } set { } }
334-
public System.Net.Connections.ConnectionFactory? ConnectionFactory { get { throw null; } set { } }
335334
public System.TimeSpan ConnectTimeout { get { throw null; } set { } }
336335
[System.Diagnostics.CodeAnalysis.AllowNullAttribute]
337336
public System.Net.CookieContainer CookieContainer { get { throw null; } set { } }
@@ -345,7 +344,6 @@ public SocketsHttpHandler() { }
345344
public int MaxConnectionsPerServer { get { throw null; } set { } }
346345
public int MaxResponseDrainSize { get { throw null; } set { } }
347346
public int MaxResponseHeadersLength { get { throw null; } set { } }
348-
public System.Func<System.Net.Http.HttpRequestMessage, System.Net.Connections.Connection, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask<System.Net.Connections.Connection>>? PlaintextFilter { get { throw null; } set { } }
349347
public System.TimeSpan PooledConnectionIdleTimeout { get { throw null; } set { } }
350348
public System.TimeSpan PooledConnectionLifetime { get { throw null; } set { } }
351349
public bool PreAuthenticate { get { throw null; } set { } }

src/libraries/System.Net.Http/ref/System.Net.Http.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<ProjectReference Include="..\..\System.Runtime\ref\System.Runtime.csproj" />
1111
<ProjectReference Include="..\..\System.Net.Primitives\ref\System.Net.Primitives.csproj" />
1212
<ProjectReference Include="..\..\System.Net.Sockets\ref\System.Net.Sockets.csproj" />
13-
<ProjectReference Include="..\..\System.Net.Connections\ref\System.Net.Connections.csproj" />
1413
<ProjectReference Include="..\..\System.Net.Security\ref\System.Net.Security.csproj" />
1514
<ProjectReference Include="..\..\System.Security.Cryptography.X509Certificates\ref\System.Security.Cryptography.X509Certificates.csproj" />
1615
<ProjectReference Include="..\..\System.Text.Encoding\ref\System.Text.Encoding.csproj" />

src/libraries/System.Net.Http/src/System.Net.Http.csproj

+1-5
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@
110110
Link="Common\System\IO\StreamHelpers.CopyValidation.cs" />
111111
<Compile Include="$(CommonPath)System\Net\Security\SslClientAuthenticationOptionsExtensions.cs"
112112
Link="Common\System\Net\Security\SslClientAuthenticationOptionsExtensions.cs" />
113-
<Compile Include="$(CommonPath)System\Net\NetworkErrorHelper.cs"
114-
Link="Common\System\Net\NetworkErrorHelper.cs" />
115113
<Compile Include="$(CommonPath)System\IO\DelegatingStream.cs"
116114
Link="Common\System\IO\DelegatingStream.cs" />
117115
<Compile Include="$(CommonPath)System\IO\ReadOnlyMemoryStream.cs"
@@ -175,9 +173,9 @@
175173
<Compile Include="System\Net\Http\SocketsHttpHandler\MultiProxy.cs" />
176174
<Compile Include="System\Net\Http\SocketsHttpHandler\RawConnectionStream.cs" />
177175
<Compile Include="System\Net\Http\SocketsHttpHandler\RedirectHandler.cs" />
176+
<Compile Include="System\Net\Http\SocketsHttpHandler\SocketsConnectionFactory.cs" />
178177
<Compile Include="System\Net\Http\SocketsHttpHandler\SocketsHttpHandler.cs" />
179178
<Compile Include="System\Net\Http\SocketsHttpHandler\SystemProxyInfo.cs" />
180-
<Compile Include="System\Net\Http\SocketsHttpHandler\DnsEndPointWithProperties.cs" />
181179
<Compile Include="$(CommonPath)System\Net\NTAuthentication.Common.cs"
182180
Link="Common\System\Net\NTAuthentication.Common.cs" />
183181
<Compile Include="$(CommonPath)System\Net\ContextFlagsPal.cs"
@@ -686,9 +684,7 @@
686684
<Reference Include="System.Diagnostics.DiagnosticSource" />
687685
<Reference Include="System.Diagnostics.Tracing" />
688686
<Reference Include="System.IO.Compression" />
689-
<Reference Include="System.IO.Pipelines" />
690687
<Reference Include="System.Memory" />
691-
<Reference Include="System.Net.Connections" />
692688
<Reference Include="System.Net.NameResolution" />
693689
<Reference Include="System.Net.NetworkInformation" />
694690
<Reference Include="System.Net.Primitives" />

src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/SocketsHttpHandler.cs

-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Threading;
77
using System.Threading.Tasks;
88
using System.Diagnostics.CodeAnalysis;
9-
using System.Net.Connections;
109

1110
namespace System.Net.Http
1211
{
@@ -149,18 +148,6 @@ public HttpKeepAlivePingPolicy KeepAlivePingPolicy
149148
set => throw new PlatformNotSupportedException();
150149
}
151150

152-
public ConnectionFactory? ConnectionFactory
153-
{
154-
get => throw new PlatformNotSupportedException();
155-
set => throw new PlatformNotSupportedException();
156-
}
157-
158-
public Func<HttpRequestMessage, Connection, CancellationToken, ValueTask<Connection>>? PlaintextFilter
159-
{
160-
get => throw new PlatformNotSupportedException();
161-
set => throw new PlatformNotSupportedException();
162-
}
163-
164151
public IDictionary<string, object?> Properties => throw new PlatformNotSupportedException();
165152

166153
public HeaderEncodingSelector<HttpRequestMessage>? RequestHeaderEncodingSelector

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs

+4-17
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33

44
using System.Diagnostics;
55
using System.IO;
6-
using System.IO.Pipelines;
7-
using System.Net.Connections;
86
using System.Net.Quic;
97
using System.Net.Security;
108
using System.Net.Sockets;
11-
using System.Runtime.ExceptionServices;
129
using System.Security.Cryptography.X509Certificates;
1310
using System.Threading;
1411
using System.Threading.Tasks;
@@ -34,11 +31,11 @@ public CertificateCallbackMapper(Func<HttpRequestMessage, X509Certificate2?, X50
3431
}
3532
}
3633

37-
public static async ValueTask<Connection> ConnectAsync(ConnectionFactory factory, DnsEndPoint endPoint, IConnectionProperties? options, CancellationToken cancellationToken)
34+
public static async ValueTask<Stream> ConnectAsync(SocketsConnectionFactory factory, DnsEndPoint endPoint, CancellationToken cancellationToken)
3835
{
3936
try
4037
{
41-
return await factory.ConnectAsync(endPoint, options, cancellationToken).ConfigureAwait(false);
38+
return await factory.ConnectAsync(endPoint, cancellationToken).ConfigureAwait(false);
4239
}
4340
catch (OperationCanceledException ex) when (ex.CancellationToken == cancellationToken)
4441
{
@@ -50,7 +47,7 @@ public static async ValueTask<Connection> ConnectAsync(ConnectionFactory factory
5047
}
5148
}
5249

53-
public static Connection Connect(string host, int port, CancellationToken cancellationToken)
50+
public static Stream Connect(string host, int port, CancellationToken cancellationToken)
5451
{
5552
// For synchronous connections, we can just create a socket and make the connection.
5653
cancellationToken.ThrowIfCancellationRequested();
@@ -63,17 +60,7 @@ public static Connection Connect(string host, int port, CancellationToken cancel
6360
socket.Connect(new DnsEndPoint(host, port));
6461
}
6562

66-
// Since we only do GracefulShutdown in SocketsHttpHandler code, Connection.FromStream() should match SocketConnection's behavior:
67-
return Connection.FromStream(new NetworkStream(socket, ownsSocket: true), localEndPoint: socket.LocalEndPoint, remoteEndPoint: socket.RemoteEndPoint);
68-
}
69-
catch (SocketException se)
70-
{
71-
socket.Dispose();
72-
73-
// SocketConnectionFactory wraps SocketException in NetworkException. Do the same here.
74-
NetworkException ne = NetworkErrorHelper.MapSocketException(se);
75-
76-
throw CreateWrappedException(ne, host, port, cancellationToken);
63+
return new NetworkStream(socket, ownsSocket: true);
7764
}
7865
catch (Exception e)
7966
{

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/DnsEndPointWithProperties.cs

-31
This file was deleted.

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Diagnostics;
88
using System.Diagnostics.CodeAnalysis;
99
using System.IO;
10-
using System.Net.Connections;
1110
using System.Net.Http.Headers;
1211
using System.Net.Http.HPack;
1312
using System.Net.Security;
@@ -24,7 +23,6 @@ internal sealed partial class Http2Connection : HttpConnectionBase, IDisposable
2423
{
2524
private readonly HttpConnectionPool _pool;
2625
private readonly Stream _stream;
27-
private readonly Connection _connection;
2826

2927
// NOTE: These are mutable structs; do not make these readonly.
3028
private ArrayBuffer _incomingBuffer;
@@ -119,11 +117,10 @@ internal enum KeepAliveState
119117
private long _keepAlivePingTimeoutTimestamp;
120118
private volatile KeepAliveState _keepAliveState;
121119

122-
public Http2Connection(HttpConnectionPool pool, Connection connection)
120+
public Http2Connection(HttpConnectionPool pool, Stream stream)
123121
{
124122
_pool = pool;
125-
_stream = connection.Stream;
126-
_connection = connection;
123+
_stream = stream;
127124
_incomingBuffer = new ArrayBuffer(InitialConnectionBufferSize);
128125
_outgoingBuffer = new ArrayBuffer(InitialConnectionBufferSize);
129126

@@ -1703,7 +1700,7 @@ private void CheckForShutdown()
17031700
GC.SuppressFinalize(this);
17041701

17051702
// Do shutdown.
1706-
_connection.Dispose();
1703+
_stream.Dispose();
17071704

17081705
_connectionWindow.Dispose();
17091706
_concurrentStreams.Dispose();

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs

+9-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using System.Net.Http.Headers;
1111
using System.Net.Security;
1212
using System.Net.Sockets;
13-
using System.Net.Connections;
1413
using System.Runtime.CompilerServices;
1514
using System.Text;
1615
using System.Threading;
@@ -46,7 +45,6 @@ internal partial class HttpConnection : HttpConnectionBase, IDisposable
4645
private readonly HttpConnectionPool _pool;
4746
private readonly Socket? _socket; // used for polling; _stream should be used for all reading/writing. _stream owns disposal.
4847
private readonly Stream _stream;
49-
private readonly Connection _connection;
5048
private readonly TransportContext? _transportContext;
5149
private readonly WeakReference<HttpConnection> _weakThisRef;
5250

@@ -73,16 +71,19 @@ internal partial class HttpConnection : HttpConnectionBase, IDisposable
7371

7472
public HttpConnection(
7573
HttpConnectionPool pool,
76-
Connection connection,
74+
Stream stream,
7775
TransportContext? transportContext)
7876
{
7977
Debug.Assert(pool != null);
80-
Debug.Assert(connection != null);
78+
Debug.Assert(stream != null);
8179

8280
_pool = pool;
83-
connection.ConnectionProperties.TryGet(out _socket); // may be null in cases where we couldn't easily get the underlying socket
84-
_stream = connection.Stream;
85-
_connection = connection;
81+
_stream = stream;
82+
if (stream is NetworkStream networkStream)
83+
{
84+
_socket = networkStream.Socket;
85+
}
86+
8687
_transportContext = transportContext;
8788

8889
_writeBuffer = new byte[InitialWriteBufferSize];
@@ -123,7 +124,7 @@ protected void Dispose(bool disposing)
123124
if (disposing)
124125
{
125126
GC.SuppressFinalize(this);
126-
_connection.Dispose();
127+
_stream.Dispose();
127128

128129
// Eat any exceptions from the read-ahead task. We don't need to log, as we expect
129130
// failures from this task due to closing the connection while a read is in progress.

0 commit comments

Comments
 (0)