Przeglądaj źródła

Add transport selector interface to Kestrel sockets and QUIC transports (#44832)

James Newton-King 3 lat temu
rodzic
commit
f543e35525

+ 6 - 1
src/Servers/Kestrel/Transport.Quic/src/QuicTransportFactory.cs

@@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Quic;
 /// <summary>
 /// A factory for QUIC based connections.
 /// </summary>
-internal sealed class QuicTransportFactory : IMultiplexedConnectionListenerFactory
+internal sealed class QuicTransportFactory : IMultiplexedConnectionListenerFactory, IConnectionListenerFactorySelector
 {
     private readonly ILogger _log;
     private readonly QuicTransportOptions _options;
@@ -56,4 +56,9 @@ internal sealed class QuicTransportFactory : IMultiplexedConnectionListenerFacto
 
         return transport;
     }
+
+    public bool CanBind(EndPoint endpoint)
+    {
+        return endpoint is IPEndPoint;
+    }
 }

+ 1 - 1
src/Servers/Kestrel/Transport.Quic/test/WebHostTests.cs

@@ -390,7 +390,7 @@ public class WebHostTests : LoggedTest
         var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => host.StartAsync()).DefaultTimeout();
 
         // Assert
-        Assert.Equal("QUIC doesn't support listening on the configured endpoint type. Expected IPEndPoint but got UnixDomainSocketEndPoint.", ex.Message);
+        Assert.Equal("No registered IMultiplexedConnectionListenerFactory supports endpoint UnixDomainSocketEndPoint: /test-path", ex.Message);
     }
 
     private static HttpClient CreateClient()

+ 1 - 0
src/Servers/Kestrel/Transport.Sockets/src/PublicAPI.Unshipped.txt

@@ -1 +1,2 @@
 #nullable enable
+Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory.CanBind(System.Net.EndPoint! endpoint) -> bool

+ 14 - 1
src/Servers/Kestrel/Transport.Sockets/src/SocketTransportFactory.cs

@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 
 using System.Net;
+using System.Net.Sockets;
 using Microsoft.AspNetCore.Connections;
 using Microsoft.Extensions.Logging;
 using Microsoft.Extensions.Options;
@@ -11,7 +12,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
 /// <summary>
 /// A factory for socket based connections.
 /// </summary>
-public sealed class SocketTransportFactory : IConnectionListenerFactory
+public sealed class SocketTransportFactory : IConnectionListenerFactory, IConnectionListenerFactorySelector
 {
     private readonly SocketTransportOptions _options;
     private readonly ILoggerFactory _logger;
@@ -40,4 +41,16 @@ public sealed class SocketTransportFactory : IConnectionListenerFactory
         transport.Bind();
         return new ValueTask<IConnectionListener>(transport);
     }
+
+    /// <inheritdoc />
+    public bool CanBind(EndPoint endpoint)
+    {
+        return endpoint switch
+        {
+            IPEndPoint _ => true,
+            UnixDomainSocketEndPoint _ => true,
+            FileHandleEndPoint _ => true,
+            _ => false
+        };
+    }
 }