コード例 #1
0
 public InternalWebHost(
     PathString appBase,
     List <IHostModule> modules,
     int port)
 {
     _appBase = appBase;
     _port    = port;
     Pages    = new ReadOnlyCollection <Page>(modules.SelectMany(x => x.Pages).ToList());
     _webHost = WebHost.CreateDefaultBuilder(new string[] {})
                .UseUrls($"http://*:{port}")
                .UseSetting(WebHostDefaults.ApplicationKey, Assembly.GetEntryAssembly().GetName().Name)
                .ConfigureLogging(factory => {
         factory.AddConsole();
     })
                .ConfigureServices(services => {
         services.AddSingleton <Startup>();
         services.AddSingleton(modules);
         services.AddSingleton(typeof(IStartup), sp =>
         {
             var hostingEnvironment = sp.GetRequiredService <IHostingEnvironment>();
             return(new ConventionBasedStartup(StartupLoader.LoadMethods(sp, typeof(Startup), hostingEnvironment.EnvironmentName)));
         });
     })
                .Build();
 }
コード例 #2
0
        // This allows code to be run on a GUI thread

        public TrayHelper(Microsoft.AspNetCore.Hosting.IWebHost _webHost)
        {
            this._webHost = _webHost;
            _webHost.Start();

            System.Drawing.Icon trayStoppedIcon;
            using (var iconStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("DeskCommandCore.Resources.trayRunning.ico"))
            {
                trayStoppedIcon = new System.Drawing.Icon(iconStream);
            }

            _components = new System.ComponentModel.Container();
            _notifyIcon = new NotifyIcon(_components)
            {
                ContextMenuStrip = new ContextMenuStrip(),
                Icon             = trayStoppedIcon,
                Text             = Resources.TrayHelper_TrayHelper_Desk_Command,
                Visible          = true,
            };

            _notifyIcon.ContextMenuStrip.Opening += ContextMenuStrip_Opening;
            _notifyIcon.MouseUp += notifyIcon_MouseUp;

            _hiddenWindow = new System.Windows.Window();
            _hiddenWindow.Hide();



            ShowRunningMessage();
        }
コード例 #3
0
        public static Microsoft.AspNetCore.Hosting.IWebHost MigrateDatabase(this Microsoft.AspNetCore.Hosting.IWebHost webHost)
        {
            using (var scope = webHost.Services.CreateScope())
            {
                var services           = scope.ServiceProvider;
                var hostingEnvironment = services.GetRequiredService <IHostingEnvironment>();
                DbContext = services.GetRequiredService <WebFacultyContext>();

                DbContext.Database.Migrate();

                CheckStaticData();
            }

            return(webHost);
        }
コード例 #4
0
        public static AspNetHosting.IWebHost EncryptSettings <TSettings>(this AspNetHosting.IWebHost webHost, bool encrypt, string settingsParamName = AppConstants.DEFAULT_SETTINGS_PARAM_NAME)
            where TSettings : class, new()
        {
            switch (encrypt)
            {
            case true:
                PerformSettingsEncryption <TSettings>(webHost.Services, settingsParamName);
                break;

            case false:
                PerformSettingsDecryption <TSettings>(webHost.Services, settingsParamName);
                break;
            }

            return(webHost);
        }
コード例 #5
0
        } // End Constructor

        void DasMulli.Win32.ServiceUtils.IWin32Service.Start(
            string[] startupArguments
            , DasMulli.Win32.ServiceUtils.ServiceStoppedCallback serviceStoppedCallback)
        {
            // in addition to the arguments that the service has been registered with,
            // each service start may add additional startup parameters.
            // To test this: Open services console, open service details, enter startup arguments and press start.
            string[] combinedArguments;
            if (startupArguments.Length > 0)
            {
                combinedArguments = new string[commandLineArguments.Length + startupArguments.Length];
                System.Array.Copy(commandLineArguments, combinedArguments, commandLineArguments.Length);
                System.Array.Copy(startupArguments, 0, combinedArguments, commandLineArguments.Length, startupArguments.Length);
            }
            else
            {
                combinedArguments = commandLineArguments;
            }


            IConfigurationRoot config = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
                                        .AddCommandLine(combinedArguments) // Microsoft.Extensions.Configuration.CommandLine.dll
                                        .Build();

            webHost = new Microsoft.AspNetCore.Hosting.WebHostBuilder()
                      .UseKestrel() // Microsoft.AspNetCore.Server.Kestrel.dll
                      .UseStartup <AspNetCoreStartup>()
                      .UseConfiguration(config)
                      .Build();


            // Make sure the windows service is stopped if the
            // ASP.NET Core stack stops for any reason
            webHost
            .Services
            .GetRequiredService <IApplicationLifetime>()   // Microsoft.Extensions.DependencyInjection
            .ApplicationStopped
            .Register(() =>
            {
                if (this.stopRequestedByWindows == false)
                {
                    serviceStoppedCallback();
                }
            });

            webHost.Start();
        } // End Sub Start
コード例 #6
0
ファイル: HostBuilder.cs プロジェクト: pauldotknopf/statik
 public InternalWebHost(
     PathString appBase,
     List <IHostModule> modules,
     int port)
 {
     _appBase = appBase;
     _port    = port;
     Pages    = new ReadOnlyCollection <Page>(modules.SelectMany(x => x.Pages).ToList());
     _webHost = WebHost.CreateDefaultBuilder(new string[] {})
                .UseUrls($"http://*:{port}")
                .UseSetting(WebHostDefaults.ApplicationKey, Assembly.GetEntryAssembly().GetName().Name)
                .ConfigureLogging(factory => {
         factory.AddConsole();
     })
                .ConfigureServices(services => {
         services.AddSingleton(modules);
     })
                .UseStartup <Startup>()
                .Build();
 }
コード例 #7
0
        public static void RunAsCustomService(this Microsoft.AspNetCore.Hosting.IWebHost host)
        {
            var webHostService = new CustomWebHostService(host);

            ServiceBase.Run(webHostService);
        }
コード例 #8
0
        public static Microsoft.AspNetCore.Hosting.IWebHost MigrateDbContext <TContext>(this Microsoft.AspNetCore.Hosting.IWebHost webHost,
                                                                                        Func <TContext, IServiceProvider, Task> seed) where TContext : Microsoft.EntityFrameworkCore.DbContext
        {
            using (var scope = webHost.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                var logger = services.GetRequiredService <Microsoft.Extensions.Logging.ILogger <TContext> >();

                var context = services.GetService <TContext>();

                try
                {
                    logger.LogInformation($"Migrating database associated with context {typeof(TContext).Name}");

                    var retry = Polly.Policy.Handle <Microsoft.Data.SqlClient.SqlException>()
                                .WaitAndRetryAsync(new[]
                    {
                        TimeSpan.FromSeconds(5),
                        TimeSpan.FromSeconds(10),
                        TimeSpan.FromSeconds(15)
                    });

                    retry.ExecuteAsync(async() =>
                    {
                        //if the sql server container is not created on run docker compose this
                        //migration can't fail for network related exception. The retry options for DbContext only
                        //apply to transient exceptions.

                        var pendingMigrations = await context.Database.GetPendingMigrationsAsync();

                        if (pendingMigrations.Any())
                        {
                            await context.Database.MigrateAsync();
                        }

                        await seed(context, services);
                    }).Wait();


                    logger.LogInformation($"Migrated database associated with context {typeof(TContext).Name}");
                }
                catch (Exception ex)
                {
                    logger.LogError(ex,
                                    $"An error occurred while migrating the database used on context {typeof(TContext).Name}");
                }
            }

            return(webHost);
        }