示例#1
0
        public static IGtkHostBuilder UseStartup <
            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
            TStartup>(this IGtkHostBuilder hostBuilder, Func <GtkHostBuilderContext, TStartup> startupFactory)
            where TStartup : class
        {
            if (startupFactory == null)
            {
                throw new ArgumentNullException(nameof(startupFactory));
            }

            // Light up the GenericWebHostBuilder implementation
            if (hostBuilder is ISupportsStartup supportsStartup)
            {
                return(supportsStartup.UseStartup(startupFactory));
            }

            return(hostBuilder
                   .ConfigureServices((context, services) =>
            {
                services.AddSingleton(typeof(IStartup), sp =>
                {
                    var instance = startupFactory(context) ??
                                   throw new InvalidOperationException(
                        "The specified factory returned null startup instance.");

                    var hostingEnvironment = sp.GetRequiredService <IHostEnvironment>();

                    // Check if the instance implements IStartup before wrapping
                    if (instance is IStartup startup)
                    {
                        return startup;
                    }

                    return new ConventionBasedStartup(StartupLoader.LoadMethods(sp, instance.GetType(),
                                                                                hostingEnvironment.EnvironmentName, instance));
                });
            }));
        }
示例#2
0
        public GenericGtkHostBuilder(IHostBuilder builder, GtkHostBuilderOptions options)
        {
            _builder = builder;
            var configBuilder = new ConfigurationBuilder()
                                .AddInMemoryCollection();

            if (options.SuppressEnvironmentConfiguration)
            {
                configBuilder.AddEnvironmentVariables(prefix: "GTKNETCORE_");
            }

            _config = configBuilder.Build();

            _builder.ConfigureHostConfiguration(config =>
            {
                config.AddConfiguration(_config);
                ExecuteHostingStartups();
            });

            _builder.ConfigureAppConfiguration((context, configurationBuilder) =>
            {
                if (_hostingStartupGtkHostBuilder is not null)
                {
                    var gtkHostContext = GetGtkHostBuilderContext(context);
                    _hostingStartupGtkHostBuilder.ConfigureAppConfiguration(gtkHostContext, configurationBuilder);
                }
            });

            _builder.ConfigureServices((context, services) =>
            {
                var gtkHostContext = GetGtkHostBuilderContext(context);
                var gtkHostOptions = (GtkHostOptions)context.Properties[typeof(GtkHostOptions)];

                services.AddSingleton(gtkHostContext.HostingEnvironment);

                services.Configure <GenericGtkHostServiceOptions>(options =>
                {
                    options.GtkHostOptions           = gtkHostOptions;
                    options.HostingStartupExceptions = _hostingStartupErrors;
                });

                services.TryAddSingleton <IApplicationBuilderFactory, ApplicationBuilderFactory>();

                _hostingStartupGtkHostBuilder?.ConfigureServices(gtkHostContext, services);

                if (!string.IsNullOrEmpty(gtkHostOptions.StartupAssembly))
                {
                    try
                    {
                        var startupType = StartupLoader.FindStartupType(gtkHostOptions.StartupAssembly,
                                                                        gtkHostContext.HostingEnvironment.EnvironmentName);
                        UseStartup(startupType, context, services);
                    }
                    catch (Exception ex) when(gtkHostOptions.CaptureStartupErrors)
                    {
                        var capture = ExceptionDispatchInfo.Capture(ex);

                        services.Configure <GenericGtkHostServiceOptions>(options =>
                        {
                            options.ConfigureApplication = app =>
                            {
                                // Throw if there was any errors initializing startup
                                capture.Throw();
                            };
                        });
                    }
                }
            });
        }
示例#3
0
        private void UseStartup([DynamicallyAccessedMembers(StartupLinkerOptions.Accessibility)]
                                Type startupType, HostBuilderContext context, IServiceCollection services, object?instance = null)
        {
            var gtkHostBuilderContext = GetGtkHostBuilderContext(context);
            var gtkHostOptions        = (GtkHostOptions)context.Properties[typeof(GtkHostOptions)];

            ExceptionDispatchInfo?startupError     = null;
            ConfigureBuilder?     configureBuilder = null;

            try
            {
                if (typeof(IStartup).IsAssignableFrom(startupType))
                {
                    throw new NotSupportedException($"{typeof(IStartup)} isn't supported");
                }

                if (StartupLoader.HasConfigureServicesIServiceProviderDelegate(startupType,
                                                                               context.HostingEnvironment.EnvironmentName))
                {
                    throw new NotSupportedException(
                              $"ConfigureServices returning an {typeof(IServiceProvider)} isn't supported.");
                }

                instance ??=
                ActivatorUtilities.CreateInstance(new HostServiceProvider(gtkHostBuilderContext), startupType);
                context.Properties[_startupKey] = instance;

                var configureServicesBuilder =
                    StartupLoader.FindConfigureServicesDelegate(startupType,
                                                                context.HostingEnvironment.EnvironmentName);
                var configureServices = configureServicesBuilder.Build(instance);

                configureServices(services);

                var configureContainerBuilder =
                    StartupLoader.FindConfigureContainerDelegate(startupType,
                                                                 context.HostingEnvironment.EnvironmentName);
                if (configureContainerBuilder.MethodInfo != null)
                {
                    var containerType = configureContainerBuilder.GetContainerType();
                    _builder.Properties[typeof(ConfigureContainerBuilder)] = configureContainerBuilder;

                    var actionType = typeof(Action <,>).MakeGenericType(typeof(HostBuilderContext), containerType);

                    var configureCallback = typeof(GenericGtkHostBuilder).GetMethod(nameof(ConfigureContainerImpl),
                                                                                    BindingFlags.NonPublic | BindingFlags.Instance) !
                                            .MakeGenericMethod(containerType)
                                            .CreateDelegate(actionType, this);

                    typeof(IHostBuilder).GetMethod(nameof(IHostBuilder.ConfigureContainer)) !
                    .MakeGenericMethod(containerType)
                    .InvokeWithoutWrappingExceptions(_builder, new object[] { configureCallback });
                }

                configureBuilder =
                    StartupLoader.FindConfigureDelegate(startupType, context.HostingEnvironment.EnvironmentName);
            }
            catch (Exception ex) when(gtkHostOptions.CaptureStartupErrors)
            {
                startupError = ExceptionDispatchInfo.Capture(ex);
            }

            services.Configure <GenericGtkHostServiceOptions>(options =>
            {
                options.ConfigureApplication = app =>
                {
                    // Throw if there was any errors initializing startup
                    startupError?.Throw();

                    // Execute Startup.Configure
                    if (instance != null && configureBuilder != null)
                    {
                        configureBuilder.Build(instance)(app);
                    }
                };
            });
        }