JHinW / Issues

self-noted
0 stars 0 forks source link

asp .net core source code: WebHostBuilder #7

Open JHinW opened 7 years ago

JHinW commented 7 years ago

https://github.com/aspnet/Hosting/blob/1ea0647ae2395252e6e8423691a544ca34491a73/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs#L38

JHinW commented 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 .

JHinW commented 7 years ago

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);
                    });
                });
        }
JHinW commented 7 years ago

continued with this code

private void EnsureStartup()
        {
            if (_startup != null)
            {
                return;
            }
             //    add by jin
             //   get startup service intance
            _startup = _hostingServiceProvider.GetRequiredService<IStartup>();
        }

see code from this link

RequestDelegate function will be built as HttpContext entry