-
-
Save configureappio/3aab13c6cacceac340b5943baad8f445 to your computer and use it in GitHub Desktop.
Snippet from WebHostBuilderExtensions to show how UseStartup registers the assembly that hosts the Startup class as the application name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Specify the startup type to be used by the web host. | |
/// </summary> | |
/// <param name="hostBuilder">The <see cref="IWebHostBuilder"/> to configure.</param> | |
/// <param name="startupType">The <see cref="Type"/> to be used.</param> | |
/// <returns>The <see cref="IWebHostBuilder"/>.</returns> | |
public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder, Type startupType) | |
{ | |
var startupAssemblyName = startupType.GetTypeInfo().Assembly.GetName().Name; | |
return hostBuilder | |
.UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName) | |
.ConfigureServices(services => | |
{ | |
if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo())) | |
{ | |
services.AddSingleton(typeof(IStartup), startupType); | |
} | |
else | |
{ | |
services.AddSingleton(typeof(IStartup), sp => | |
{ | |
var hostingEnvironment = sp.GetRequiredService<IHostingEnvironment>(); | |
return new ConventionBasedStartup(StartupLoader.LoadMethods(sp, startupType, hostingEnvironment.EnvironmentName)); | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment