internal static void RunService(StartupArguments param, Action <IHostBuilder> configureBuilder) { ServiceStartup srv = new ServiceStartup(); srv.Configure = param.Configure; srv.Init(); var builder = Host.CreateDefaultBuilder().ConfigureServices((hostContext, services) => { services.AddHostedService(s => srv); var cf = NativeSettings.Configuration.GetSection("HostOptions"); services.Configure <HostOptions>(cf.GetSection("HostOptions")); }); configureBuilder(builder); host = builder.Build(); if (param.Debug) { host.RunAsync(); Console.ReadLine(); //host.StopAsync(); host.Dispose(); } else { host.Run(); } }
/// <summary> /// runs the Service /// </summary> /// <param name="parameters">Startup Parameters of the Application</param> /// <param name="configureHost">callback to configure the host, before it is started</param> public static void Run(string[] parameters, Action <IHostBuilder> configureHost) { var parser = new CommandLineParser.CommandLineParser(typeof(StartupArguments), false); var arg = new StartupArguments(); parser.Configure(parameters, arg); try { AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; Console.WriteLine("Current Platform is: {0}", IntPtr.Size == 4 ? "x86" : IntPtr.Size == 8 ? "x64" : "unkonwn"); if (arg.Install) { var locationRaw = Assembly.GetEntryAssembly().Location; var exeLocation = Path.Combine(Path.GetDirectoryName(locationRaw), Path.GetFileNameWithoutExtension(locationRaw)); File.WriteAllText($"{Assembly.GetEntryAssembly().Location}.install.bat", $"sc.exe create \"{ServiceConfigHelper.ServiceName}\" binpath=\"{exeLocation}.exe\" start={ToStartType(ServiceConfigHelper.StartType)} displayname=\"{ServiceConfigHelper.DisplayName ?? ServiceConfigHelper.ServiceName}\" {((ServiceConfigHelper.Dependencies?.Count ?? 0) == 0 ? "" : $"depend=\"{string.Join("/", ServiceConfigHelper.Dependencies)}\"")}"); Console.WriteLine($"{Assembly.GetEntryAssembly().Location}.install.bat ausführen."); } else if (arg.UnInstall) { File.WriteAllText($"{Assembly.GetEntryAssembly().Location}.uninstall.bat", $"sc.exe uninstall \"{ServiceConfigHelper.ServiceName}\""); Console.WriteLine($"{Assembly.GetEntryAssembly().Location}.uninstall.bat ausführen."); } else if (arg.Help) { Console.WriteLine(parser.PrintUsage(true, 10, 75)); } else { ServiceStartup.RunService(arg, configureHost); } } catch (CommandLineSyntaxException ex) { Console.WriteLine(ex.Message); Console.WriteLine(parser.PrintUsage(true, 10, 75)); } catch (Exception ex) { Console.WriteLine(ex.OutlineException()); } }