From 75368e2d448d34180e55857cfd98aa080435d309 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Thu, 4 Jul 2024 14:35:01 +0800 Subject: [PATCH] Add named pipes methods to Kestrel configuration loader (#56563) --- .../Core/src/KestrelConfigurationLoader.cs | 21 +++++++++++++++++++ .../Kestrel/Core/src/PublicAPI.Unshipped.txt | 2 ++ .../test/KestrelConfigurationLoaderTests.cs | 17 +++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/Servers/Kestrel/Core/src/KestrelConfigurationLoader.cs b/src/Servers/Kestrel/Core/src/KestrelConfigurationLoader.cs index 9b71c5127fcd..49842d4c3389 100644 --- a/src/Servers/Kestrel/Core/src/KestrelConfigurationLoader.cs +++ b/src/Servers/Kestrel/Core/src/KestrelConfigurationLoader.cs @@ -191,6 +191,27 @@ public KestrelConfigurationLoader UnixSocketEndpoint(string socketPath, Action + /// Bind to given named pipe. + /// + public KestrelConfigurationLoader NamedPipeEndpoint(string pipeName) => NamedPipeEndpoint(pipeName, _ => { }); + + /// + /// Bind to given named pipe. + /// + public KestrelConfigurationLoader NamedPipeEndpoint(string pipeName, Action configure) + { + ArgumentNullException.ThrowIfNull(pipeName); + ArgumentNullException.ThrowIfNull(configure); + + EndpointsToAdd.Add(() => + { + Options.ListenNamedPipe(pipeName, configure); + }); + + return this; + } + /// /// Open a socket file descriptor. /// diff --git a/src/Servers/Kestrel/Core/src/PublicAPI.Unshipped.txt b/src/Servers/Kestrel/Core/src/PublicAPI.Unshipped.txt index 7dc5c58110bf..562e688fc91b 100644 --- a/src/Servers/Kestrel/Core/src/PublicAPI.Unshipped.txt +++ b/src/Servers/Kestrel/Core/src/PublicAPI.Unshipped.txt @@ -1 +1,3 @@ #nullable enable +Microsoft.AspNetCore.Server.Kestrel.KestrelConfigurationLoader.NamedPipeEndpoint(string! pipeName) -> Microsoft.AspNetCore.Server.Kestrel.KestrelConfigurationLoader! +Microsoft.AspNetCore.Server.Kestrel.KestrelConfigurationLoader.NamedPipeEndpoint(string! pipeName, System.Action! configure) -> Microsoft.AspNetCore.Server.Kestrel.KestrelConfigurationLoader! diff --git a/src/Servers/Kestrel/Kestrel/test/KestrelConfigurationLoaderTests.cs b/src/Servers/Kestrel/Kestrel/test/KestrelConfigurationLoaderTests.cs index d5138fd5688d..641ab4d4d079 100644 --- a/src/Servers/Kestrel/Kestrel/test/KestrelConfigurationLoaderTests.cs +++ b/src/Servers/Kestrel/Kestrel/test/KestrelConfigurationLoaderTests.cs @@ -1844,6 +1844,23 @@ public void ReloadDoesNotAddEndpoints() _ = serverOptions.ConfigurationLoader.Reload(); } + [Fact] + public void AddNamedPipeEndpoint() + { + var serverOptions = CreateServerOptions(); + var builder = serverOptions.Configure() + .NamedPipeEndpoint("abc"); + + Assert.Empty(serverOptions.GetListenOptions()); + Assert.Equal(builder, serverOptions.ConfigurationLoader); + + builder.Load(); + + Assert.Single(serverOptions.GetListenOptions()); + Assert.Equal("abc", serverOptions.CodeBackedListenOptions[0].PipeName); + Assert.NotNull(serverOptions.ConfigurationLoader); + } + private static string GetCertificatePath() { var appData = Environment.GetEnvironmentVariable("APPDATA");