dolittle-obsolete / DotNET.Fundamentals

Reusable, fundamental abstractions and building blocks
http://www.dolittle.io
MIT License
4 stars 8 forks source link

Host builder setup #303

Closed jakhog closed 4 years ago

jakhog commented 4 years ago

This PR contains a ServiceProviderFactory that uses the AutofacServiceProviderFactory and the Dolittle booting process to set up the container, and to run the boot procedures.

In principle - it performs the following steps:

  1. Creates a container builder using Autofac with the configured ServiceCollection. The service collection is typically set up in a Startup class, and through other HostBuilder extension methods.
  2. Runs the first part of of the Dolittle booting (until the BootProcedures) to discover bindings and set up the initial system.
  3. Adds the discovered bindings to the container builder.
  4. Creates the IContainer that uses the ServiceProvider to create instances, and notifies Dolittle internals that the container is ready.
  5. Discovers logging providers (including the ILoggerFactory with some workarounds to help developers) and configures the logging system to use these new logging providers from now on and flushes logs captured during booting.
  6. Runs the BootProcedures to finalise the Dolittle booting process.

There is also a IHostBuilder extension method called .UseDolittle() that configures the host builder to use the ServiceProviderFactory described above. Typical use of this would be in a Program class:

    static class Program
    {
        public static async Task Main(string[] args) =>
            await CreateHostBuilder(args)
                .Build()
                .RunAsync().ConfigureAwait(false);

        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                .UseDolittle()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder
                        .UseKestrel()
                        .UseStartup<Startup>();
                });
        }
    }

┆Issue is synchronized with this Asana task

jakhog commented 4 years ago

@woksin: the AspNetCore Bootstrap needs to be changed so that it doesn't do that anymore. Essentially it should just add AspNetCore specific things - like the ExecutionContext middleware, Command/Query-coordinators etc.