Skip to content

Commit

Permalink
Merge pull request #55 from fhofmann/parameter-startimmediately
Browse files Browse the repository at this point in the history
Added parameter to control if the service starts immediately when installing
  • Loading branch information
PeterKottas authored Nov 27, 2017
2 parents 9ffc588 + 21b4d0e commit a58da48
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public HostConfiguration()

public string DisplayName { get; set; }

public bool StartImmediately { get; set; }

public SERVICE Service { get; set; }

public Func<List<string>, IMicroServiceController, SERVICE> ServiceFactory { get; set; }
Expand Down
68 changes: 40 additions & 28 deletions Source/PeterKottas.DotNetCore.WindowsService/ServiceRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class ServiceRunner<SERVICE> where SERVICE : IMicroService
public static int Run(Action<HostConfigurator<SERVICE>> runAction)
{
Directory.SetCurrentDirectory(PlatformServices.Default.Application.ApplicationBasePath);

var innerConfig = new HostConfiguration<SERVICE>();
innerConfig.Action = ActionEnum.RunInteractive;
innerConfig.Name = typeof(SERVICE).FullName;
Expand All @@ -44,6 +44,18 @@ public static int Run(Action<HostConfigurator<SERVICE>> runAction)
}
});
config.AddParameter(new CmdArgParam()
{
Key = "startimmediately",
Description = "Start the service immediately when installing.",
Value = val =>
{
if (bool.TryParse(val, out var startImmediately))
{
innerConfig.StartImmediately = startImmediately;
}
}
});
config.AddParameter(new CmdArgParam()
{
Key = "name",
Description = "Service name",
Expand Down Expand Up @@ -130,36 +142,36 @@ public static int Run(Action<HostConfigurator<SERVICE>> runAction)
runAction(hostConfiguration);
if (innerConfig.Action == ActionEnum.Run)
innerConfig.Service = innerConfig.ServiceFactory(innerConfig.ExtraArguments,
new MicroServiceController(() =>
{
var task = Task.Factory.StartNew(() =>
{
UsingServiceController(innerConfig, (sc, cfg) => StopService(cfg, sc));
});
}
));
else if (innerConfig.Action == ActionEnum.RunInteractive)
{
var consoleService = new InnerService(innerConfig.Name, () => Start(innerConfig), () => Stop(innerConfig));
var consoleHost = new ConsoleServiceHost<SERVICE>(consoleService, innerConfig);
new MicroServiceController(() =>
{
var task = Task.Factory.StartNew(() =>
{
UsingServiceController(innerConfig, (sc, cfg) => StopService(cfg, sc));
});
}
));
else if (innerConfig.Action == ActionEnum.RunInteractive)
{
var consoleService = new InnerService(innerConfig.Name, () => Start(innerConfig), () => Stop(innerConfig));
var consoleHost = new ConsoleServiceHost<SERVICE>(consoleService, innerConfig);

innerConfig.Service = innerConfig.ServiceFactory(innerConfig.ExtraArguments,
new MicroServiceController(() =>
{
var task = Task.Factory.StartNew(() =>
{
consoleHost.StopService();
});
}
));
innerConfig.Service = innerConfig.ServiceFactory(innerConfig.ExtraArguments,
new MicroServiceController(() =>
{
var task = Task.Factory.StartNew(() =>
{
consoleHost.StopService();
});
}
));

// Return the console host run result, so we get some idea what failed if result is not OK
return (int)consoleHost.Run();
}
// Return the console host run result, so we get some idea what failed if result is not OK
return (int)consoleHost.Run();
}

ConfigureService(innerConfig);
ConfigureService(innerConfig);

return 0;
return 0;
}
catch (Exception e)
{
Expand Down Expand Up @@ -203,7 +215,7 @@ private static void Install(HostConfiguration<SERVICE> config, ServiceController
GetServiceCommand(config.ExtraArguments),
cred,
autoStart: true,
startImmediately: true,
startImmediately: config.StartImmediately,
errorSeverity: ErrorSeverity.Normal);
Console.WriteLine($@"Successfully registered and started service ""{config.Name}"" (""{config.Description}"")");
}
Expand Down

0 comments on commit a58da48

Please sign in to comment.