From cfd20ad2d71ff63dc9a06be8bf8628321e5bac99 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Tue, 4 Aug 2020 12:40:42 -0700 Subject: [PATCH] Fix some linker warnings, report others (#24553) * Fix some linker warnings, report others - Added RequiresUnreferencedCode to UseStartup that takes a string - Added some suppressions - Fixed ConfigureContainer calls - Fixed HostingStartupAttribute --- .../src/HostingAbstractionsWebHostBuilderExtensions.cs | 3 ++- src/Hosting/Abstractions/src/HostingStartupAttribute.cs | 8 +++++--- .../Hosting/src/GenericHost/GenericWebHostBuilder.cs | 7 ++++--- src/Hosting/Hosting/src/Internal/StartupLoader.cs | 1 + .../Server.IntegrationTesting/src/ApplicationPublisher.cs | 7 ++++--- .../src/Common/DeploymentParameters.cs | 5 +++++ .../test/FunctionalTests/LinkedApplicationTests.cs | 1 + 7 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs b/src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs index 3b14957ba4..04f9ca6d65 100644 --- a/src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs +++ b/src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; using System.Threading; @@ -47,6 +48,7 @@ namespace Microsoft.AspNetCore.Hosting /// The to configure. /// The name of the assembly containing the startup type. /// The . + [RequiresUnreferencedCode("Types and members the loaded assembly depends on might be removed.")] public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder, string startupAssemblyName) { if (startupAssemblyName == null) @@ -54,7 +56,6 @@ namespace Microsoft.AspNetCore.Hosting throw new ArgumentNullException(nameof(startupAssemblyName)); } - return hostBuilder .UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName) .UseSetting(WebHostDefaults.StartupAssemblyKey, startupAssemblyName); diff --git a/src/Hosting/Abstractions/src/HostingStartupAttribute.cs b/src/Hosting/Abstractions/src/HostingStartupAttribute.cs index cb028c327b..b03ec263dc 100644 --- a/src/Hosting/Abstractions/src/HostingStartupAttribute.cs +++ b/src/Hosting/Abstractions/src/HostingStartupAttribute.cs @@ -1,7 +1,8 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; +using System.Diagnostics.CodeAnalysis; using System.Reflection; namespace Microsoft.AspNetCore.Hosting @@ -16,7 +17,7 @@ namespace Microsoft.AspNetCore.Hosting /// Constructs the with the specified type. /// /// A type that implements . - public HostingStartupAttribute(Type hostingStartupType) + public HostingStartupAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type hostingStartupType) { if (hostingStartupType == null) { @@ -35,6 +36,7 @@ namespace Microsoft.AspNetCore.Hosting /// The implementation of that should be loaded when /// starting an application. /// + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] public Type HostingStartupType { get; } } -} \ No newline at end of file +} diff --git a/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs b/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs index d3b97ca8f1..9f97f03c4e 100644 --- a/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs +++ b/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs @@ -234,6 +234,7 @@ namespace Microsoft.AspNetCore.Hosting return this; } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", Justification = "We need to call a generic method on IHostBuilder.")] private void UseStartup([DynamicallyAccessedMembers(StartupLinkerOptions.Accessibility)] Type startupType, HostBuilderContext context, IServiceCollection services, object instance = null) { var webHostBuilderContext = GetWebHostBuilderContext(context); @@ -275,12 +276,12 @@ namespace Microsoft.AspNetCore.Hosting var actionType = typeof(Action<,>).MakeGenericType(typeof(HostBuilderContext), containerType); // Get the private ConfigureContainer method on this type then close over the container type - var configureCallback = GetType().GetMethod(nameof(ConfigureContainer), BindingFlags.NonPublic | BindingFlags.Instance) + var configureCallback = typeof(GenericWebHostBuilder).GetMethod(nameof(ConfigureContainerImpl), BindingFlags.NonPublic | BindingFlags.Instance) .MakeGenericMethod(containerType) .CreateDelegate(actionType, this); // _builder.ConfigureContainer(ConfigureContainer); - typeof(IHostBuilder).GetMethods().First(m => m.Name == nameof(IHostBuilder.ConfigureContainer)) + typeof(IHostBuilder).GetMethod(nameof(IHostBuilder.ConfigureContainer)) .MakeGenericMethod(containerType) .InvokeWithoutWrappingExceptions(_builder, new object[] { configureCallback }); } @@ -310,7 +311,7 @@ namespace Microsoft.AspNetCore.Hosting }); } - private void ConfigureContainer(HostBuilderContext context, TContainer container) + private void ConfigureContainerImpl(HostBuilderContext context, TContainer container) { var instance = context.Properties[_startupKey]; var builder = (ConfigureContainerBuilder)context.Properties[typeof(ConfigureContainerBuilder)]; diff --git a/src/Hosting/Hosting/src/Internal/StartupLoader.cs b/src/Hosting/Hosting/src/Internal/StartupLoader.cs index 8e8f235eae..163c942269 100644 --- a/src/Hosting/Hosting/src/Internal/StartupLoader.cs +++ b/src/Hosting/Hosting/src/Internal/StartupLoader.cs @@ -222,6 +222,7 @@ namespace Microsoft.AspNetCore.Hosting } } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "We're warning at the entry point. This is an implementation detail.")] public static Type FindStartupType(string startupAssemblyName, string environmentName) { if (string.IsNullOrEmpty(startupAssemblyName)) diff --git a/src/Hosting/Server.IntegrationTesting/src/ApplicationPublisher.cs b/src/Hosting/Server.IntegrationTesting/src/ApplicationPublisher.cs index 5ef431c2ce..5f64d971c2 100644 --- a/src/Hosting/Server.IntegrationTesting/src/ApplicationPublisher.cs +++ b/src/Hosting/Server.IntegrationTesting/src/ApplicationPublisher.cs @@ -79,8 +79,9 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting // https://stackoverflow.com/a/37983587/102052 // // 2. If "dotnet publish" does hang indefinitely for some reason, tests should fail fast with an error message. - const int timeoutMinutes = 5; - if (hostProcess.WaitForExit(milliseconds: timeoutMinutes * 60 * 1000)) + var timeout = deploymentParameters.PublishTimeout ?? TimeSpan.FromMinutes(5); + + if (hostProcess.WaitForExit(milliseconds: (int)timeout.TotalMilliseconds)) { if (hostProcess.ExitCode != 0) { @@ -91,7 +92,7 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting } else { - var message = $"{DotnetCommandName} publish failed to exit after {timeoutMinutes} minutes"; + var message = $"{DotnetCommandName} publish failed to exit after {timeout.TotalMinutes} minutes"; logger.LogError(message); throw new Exception(message); } diff --git a/src/Hosting/Server.IntegrationTesting/src/Common/DeploymentParameters.cs b/src/Hosting/Server.IntegrationTesting/src/Common/DeploymentParameters.cs index 5002e8f530..0e26e772a0 100644 --- a/src/Hosting/Server.IntegrationTesting/src/Common/DeploymentParameters.cs +++ b/src/Hosting/Server.IntegrationTesting/src/Common/DeploymentParameters.cs @@ -179,6 +179,11 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting /// public Action UserAdditionalCleanup { get; set; } + /// + /// Timeout for publish + /// + public TimeSpan? PublishTimeout { get; set; } + public override string ToString() { return string.Format( diff --git a/src/Hosting/test/FunctionalTests/LinkedApplicationTests.cs b/src/Hosting/test/FunctionalTests/LinkedApplicationTests.cs index 8bce9c2b42..7b2de2b17b 100644 --- a/src/Hosting/test/FunctionalTests/LinkedApplicationTests.cs +++ b/src/Hosting/test/FunctionalTests/LinkedApplicationTests.cs @@ -37,6 +37,7 @@ namespace Microsoft.AspNetCore.Hosting.FunctionalTests ApplicationType = ApplicationType.Standalone, PublishApplicationBeforeDeployment = true, RestoreDependencies = true, + PublishTimeout = TimeSpan.FromMinutes(10), // Machines are slow (these tests restore) StatusMessagesEnabled = false };