Open JHinW opened 7 years ago
read code snippet
var host = new WebHostBuilder()
//.UseConfiguration(config) // Default set of configurations to use, may be subsequently overridden
//.UseKestrel()
//.UseFakeServer()
//.UseContentRoot(Directory.GetCurrentDirectory()) // Override the content root with the current directory
.UseUrls("http://*:1000", "https://*:902")
//.UseEnvironment(EnvironmentName.Development)
//.UseWebRoot("public")
// .ConfigureServices(services =>
//{
// Configure services that the application can see
// services.AddSingleton<IMyCustomService, MyCustomService>();
//})
.Configure(app =>
{
// Write the application inline, this won't call any startup class in the assembly
app.Use(next => context =>
{
return next(context);
});
})
.Build();
function named Configure will accept a function(which will return an ApplicaitonBuilder instance) as parameter .
continued with this code
with code above, webhost service can get an instance via index IStartup
public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Action<IApplicationBuilder> configureApp)
{
if (configureApp == null)
{
throw new ArgumentNullException(nameof(configureApp));
}
var startupAssemblyName = configureApp.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name;
return hostBuilder
.UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName)
.ConfigureServices(services =>
{
// add by jin
// you can get this instance from ServiceProvider
services.AddSingleton<IStartup>(sp =>
{
return new DelegateStartup(sp.GetRequiredService<IServiceProviderFactory<IServiceCollection>>(), configureApp);
});
});
}
https://github.com/aspnet/Hosting/blob/1ea0647ae2395252e6e8423691a544ca34491a73/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs#L38