private static IHostBuilder CreateDefaultHostBuilder(Type startupType)
        {
            // Define the root application builder, used for defining middleware and other application components
            UOWApplication applicationBuilder = new UOWApplication();
            AutofacServiceProviderFactory autofacServiceProviderFactory = new AutofacServiceProviderFactory();

            // Create a root serviceProvider and add the Startup type into the container
            ServiceCollection serviceDescriptors = new ServiceCollection();

            serviceDescriptors.AddSingleton <UOWApplication>(applicationBuilder);

            ContainerBuilder builder = autofacServiceProviderFactory.CreateBuilder(serviceDescriptors);

            if (startupType != null)
            {
                builder.RegisterType(startupType).SingleInstance().AsSelf().As <UOWStartup>();
            }

            IServiceProvider rootServiceProvider = autofacServiceProviderFactory.CreateServiceProvider(builder);

            // Get the root lifetime scope to be used as the appHost's parent container
            ILifetimeScope rootLifetimeScope = rootServiceProvider.GetRequiredService <ILifetimeScope>();

            IHostBuilder appHostBuilder = Host.CreateDefaultBuilder()
                                          .UseWindowsService()
                                          .UseServiceProviderFactory(new AutofacChildLifetimeScopeServiceProviderFactory(rootLifetimeScope))
                                          .ConfigureServices(ConfigureServices);

            if (startupType != null)
            {
                // Resolve the Startup type so the end use can inject into the constructor
                UOWStartup startup = rootServiceProvider.GetRequiredService(startupType) as UOWStartup;

                // Startup may register some dependencies
                appHostBuilder.ConfigureServices(startup.ConfigureServices);
                applicationBuilder.Startup = startup;
            }

            return(appHostBuilder);
        }
Exemple #2
0
 public virtual void Configure(UOWApplication application)
 {
 }