Exemplo n.º 1
0
        protected override async void OnStartup(StartupEventArgs e)
        {
            _applicationBootstrapper.BeforeStart();

            // Prevent WPF tooltips from expiration
            ToolTipService.ShowDurationProperty.OverrideMetadata(typeof(DependencyObject), new FrameworkPropertyMetadata(int.MaxValue));

            await Task.WhenAll(OnStartupAsync(), _applicationBootstrapper.OnStartAsync()).ConfigureAwait(true);
        }
Exemplo n.º 2
0
        public WepApiLauncher(
            Action <ContainerBuilder, IConfiguration>?registerDependencies = null,
            Func <IHostBuilder, IHostBuilder>?configureHost = null,
            Action <IServiceCollection>?configureServices   = null,
            Action <HostBuilderContext, ILoggingBuilder>?configureLogging = null,
            string?alreadyRunningMessage             = null,
            int waitAfterOldInstanceKillMilliseconds = 0,
            NewInstanceHandling newInstanceHandling  = NewInstanceHandling.AllowMultiple,
            CultureInfo?startupCulture = null,
            Assembly?webApiAssembly    = null,
            IReadOnlyCollection <Assembly>?additionalXmlCommentsAssemblies = null)
        {
            IConfiguration?configuration         = null;
            var            cultureManager        = new ConsoleCultureManager();
            var            applicationTerminator = new ConsoleApplicationTerminator();

            _assemblyInfoProvider = new AssemblyInfoProvider(new EntryAssemblyProvider(), new SpecialPathsProvider());
            webApiAssembly ??= Assembly.GetCallingAssembly();
            var baseDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule?.FileName) ?? throw new InvalidOperationException("Cannot get base directory");

            Directory.SetCurrentDirectory(baseDirectory);
            _applicationBootstrapper = new ApplicationStartupBootstrapper(
                cultureManager,
                applicationTerminator,
                message => { },
                CreateMutex,
                containerBuilder => registerDependencies?.Invoke(containerBuilder, configuration ?? throw new InvalidOperationException("Configuration was not initialized")),
                _assemblyInfoProvider,
                hostBuilder =>
            {
                configureHost?.Invoke(hostBuilder);
                hostBuilder.UseConsoleLifetime();
                return(ApiHostingHelper.RegisterWebApiHost(hostBuilder, baseDirectory: baseDirectory, applicationKey: webApiAssembly.GetName().Name));
            },
                services =>
            {
                // Build the intermediate service provider
                var sp          = services.BuildServiceProvider();
                configuration   = sp.GetRequiredService <IConfiguration>();
                var appSettings = configuration.GetSection("AppSettings");
                var appName     = appSettings[AppSettingsConstants.AppNameKey];
                var appVersion  = appSettings[AppSettingsConstants.AppVersionKey];

                ApiHostingHelper.RegisterServices(
                    services,
                    webApiAssembly,
                    configureMvc: mvcBuilder => mvcBuilder.AddJsonOptions(
                        options =>
                {
                    options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
                    options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
                }),
                    configureSwagger: swaggerGenOptions =>
                {
                    swaggerGenOptions.SwaggerDoc("v1", new OpenApiInfo {
                        Title = appName, Version = appVersion
                    });

                    var xmlDocAssemblies = new List <Assembly>
                    {
                        webApiAssembly
                    };
                    if (additionalXmlCommentsAssemblies != null)
                    {
                        xmlDocAssemblies.AddRange(additionalXmlCommentsAssemblies);
                    }

                    foreach (var xmlFilePath in xmlDocAssemblies.Select(assembly => assembly.GetName().Name + ".xml")
                             .Select(xmlFileName => Path.Combine(baseDirectory, xmlFileName))
                             .Where(File.Exists))
                    {
                        swaggerGenOptions.IncludeXmlComments(xmlFilePath);
                    }

                    // If there are two similar routes - this will fix the collision by choosing the first one
                    swaggerGenOptions.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
                })
                .Configure <ConsoleLifetimeOptions>(options => options.SuppressStatusMessages = true)
                .AddHttpContextAccessor();
                configureServices?.Invoke(services);
            },
                (hostBuilderContext, loggingBuilder) =>
            {
                SetupLogging(baseDirectory, hostBuilderContext.HostingEnvironment.EnvironmentName, LogEventLevel.Debug);
                loggingBuilder.AddSerilog();
                configureLogging?.Invoke(hostBuilderContext, loggingBuilder);
            },
                alreadyRunningMessage,
                waitAfterOldInstanceKillMilliseconds,
                newInstanceHandling,
                startupCulture,
                baseDirectory);
            _applicationBootstrapper.BeforeStart();
            Configuration = configuration ?? throw new InvalidOperationException("Configuration was not initialized");
        }