From e4cf12017b90a28379650987b58c3d77cdb2a5cb Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Thu, 29 Sep 2016 09:43:03 -0700 Subject: [PATCH] Make UseIISIntegration idempotent (#274) --- .../Properties/AssemblyInfo.cs | 3 ++ .../WebHostBuilderIISExtensions.cs | 9 +++++ .../IISExtensionTests.cs | 33 +++++++++++++++++++ .../project.json | 3 ++ 4 files changed, 48 insertions(+) create mode 100644 test/Microsoft.AspNetCore.Server.IISIntegration.Tests/IISExtensionTests.cs diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration/Properties/AssemblyInfo.cs b/src/Microsoft.AspNetCore.Server.IISIntegration/Properties/AssemblyInfo.cs index 76feceeff0..9cf586c3a8 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration/Properties/AssemblyInfo.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration/Properties/AssemblyInfo.cs @@ -3,6 +3,9 @@ using System.Reflection; using System.Resources; +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Server.IISIntegration.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] [assembly: AssemblyMetadata("Serviceable", "True")] [assembly: NeutralResourcesLanguage("en-us")] diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration/WebHostBuilderIISExtensions.cs b/src/Microsoft.AspNetCore.Server.IISIntegration/WebHostBuilderIISExtensions.cs index d6746b1f16..ae6a336979 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration/WebHostBuilderIISExtensions.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration/WebHostBuilderIISExtensions.cs @@ -30,12 +30,21 @@ namespace Microsoft.AspNetCore.Hosting throw new ArgumentNullException(nameof(hostBuilder)); } + // Check if `UseIISIntegration` was called already + if (hostBuilder.GetSetting(nameof(UseIISIntegration)) != null) + { + return hostBuilder; + } + var port = hostBuilder.GetSetting(ServerPort) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{ServerPort}"); var path = hostBuilder.GetSetting(ServerPath) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{ServerPath}"); var pairingToken = hostBuilder.GetSetting(PairingToken) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{PairingToken}"); if (!string.IsNullOrEmpty(port) && !string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(pairingToken)) { + // Set flag to prevent double service configuration + hostBuilder.UseSetting(nameof(UseIISIntegration), true.ToString()); + var address = "http://localhost:" + port; hostBuilder.CaptureStartupErrors(true); diff --git a/test/Microsoft.AspNetCore.Server.IISIntegration.Tests/IISExtensionTests.cs b/test/Microsoft.AspNetCore.Server.IISIntegration.Tests/IISExtensionTests.cs new file mode 100644 index 0000000000..bf4a0e6bfd --- /dev/null +++ b/test/Microsoft.AspNetCore.Server.IISIntegration.Tests/IISExtensionTests.cs @@ -0,0 +1,33 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Linq; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace Microsoft.AspNetCore.Server.IISIntegration +{ + public class IISExtensionTests + { + [Fact] + public void CallingUseIISIntegrationMultipleTimesWorks() + { + + var builder = new WebHostBuilder() + .UseSetting("TOKEN", "TestToken") + .UseSetting("PORT", "12345") + .UseSetting("APPL_PATH", "/") + .UseIISIntegration() + .UseIISIntegration() + .Configure(app => { }); + var server = new TestServer(builder); + + var filters = server.Host.Services.GetServices() + .OfType(); + + Assert.Equal(1, filters.Count()); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Server.IISIntegration.Tests/project.json b/test/Microsoft.AspNetCore.Server.IISIntegration.Tests/project.json index f93d7caafa..1c95f3f48f 100644 --- a/test/Microsoft.AspNetCore.Server.IISIntegration.Tests/project.json +++ b/test/Microsoft.AspNetCore.Server.IISIntegration.Tests/project.json @@ -1,5 +1,8 @@ { "version": "1.1.0-*", + "buildOptions": { + "keyFile": "../../tools/Key.snk" + }, "dependencies": { "dotnet-test-xunit": "2.2.0-*", "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0-*",