Skip to content

Commit

Permalink
WindowsServiceHost will use HostConfigurator to apply service start a…
Browse files Browse the repository at this point in the history
…rguments using command line definitions.
  • Loading branch information
jozefizso authored and phatboyg committed Apr 30, 2015
1 parent e6d72f0 commit 922fbff
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ namespace Topshelf.HostConfigurators
{
using Builders;

public delegate EnvironmentBuilder EnvironmentBuilderFactory();
public delegate EnvironmentBuilder EnvironmentBuilderFactory(HostConfigurator configurator);
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public Host CreateHost()
.InfoFormat("{0} v{1}, .NET Framework v{2}", type.Namespace, type.Assembly.GetName().Version,
Environment.Version);

EnvironmentBuilder environmentBuilder = _environmentBuilderFactory();
EnvironmentBuilder environmentBuilder = _environmentBuilderFactory(this);

HostEnvironment environment = environmentBuilder.Build();

Expand Down Expand Up @@ -224,9 +224,9 @@ static HostBuilder DefaultHostBuilderFactory(HostEnvironment environment, HostSe
return new RunBuilder(environment, settings);
}

static EnvironmentBuilder DefaultEnvironmentBuilderFactory()
static EnvironmentBuilder DefaultEnvironmentBuilderFactory(HostConfigurator configurator)
{
return new WindowsHostEnvironmentBuilder();
return new WindowsHostEnvironmentBuilder(configurator);
}
}
}
9 changes: 8 additions & 1 deletion src/Topshelf/Runtime/Windows/WindowsHostEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ namespace Topshelf.Runtime.Windows
using System.Security.Principal;
using System.ServiceProcess;
using Logging;
using Topshelf.HostConfigurators;

public class WindowsHostEnvironment :
HostEnvironment
{
readonly LogWriter _log = HostLogger.Get(typeof(WindowsHostEnvironment));
private HostConfigurator _hostConfigurator;

public WindowsHostEnvironment(HostConfigurator configurator)
{
_hostConfigurator = configurator;
}

public bool IsServiceInstalled(string serviceName)
{
Expand Down Expand Up @@ -180,7 +187,7 @@ public bool RunAsAdministrator()

public Host CreateServiceHost(HostSettings settings, ServiceHandle serviceHandle)
{
return new WindowsServiceHost(this, settings, serviceHandle);
return new WindowsServiceHost(this, settings, serviceHandle, this._hostConfigurator);
}

public void SendServiceCommand(string serviceName, int command)
Expand Down
10 changes: 9 additions & 1 deletion src/Topshelf/Runtime/Windows/WindowsHostEnvironmentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@
namespace Topshelf.Runtime.Windows
{
using Builders;
using Topshelf.HostConfigurators;

public class WindowsHostEnvironmentBuilder :
EnvironmentBuilder
{
HostConfigurator _hostConfigurator;

public WindowsHostEnvironmentBuilder(HostConfigurator configurator)
{
_hostConfigurator = configurator;
}

public HostEnvironment Build()
{
return new WindowsHostEnvironment();
return new WindowsHostEnvironment(_hostConfigurator);
}
}
}
9 changes: 7 additions & 2 deletions src/Topshelf/Runtime/Windows/WindowsServiceHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Topshelf.Runtime.Windows
using System.Threading.Tasks;
#endif
using Logging;

using Topshelf.HostConfigurators;

public class WindowsServiceHost :
ServiceBase,
Expand All @@ -32,11 +32,12 @@ public class WindowsServiceHost :
readonly HostEnvironment _environment;
readonly ServiceHandle _serviceHandle;
readonly HostSettings _settings;
readonly HostConfigurator _configurator;
int _deadThread;
bool _disposed;
Exception _unhandledException;

public WindowsServiceHost(HostEnvironment environment, HostSettings settings, ServiceHandle serviceHandle)
public WindowsServiceHost(HostEnvironment environment, HostSettings settings, ServiceHandle serviceHandle, HostConfigurator configurator)
{
if (settings == null)
throw new ArgumentNullException("settings");
Expand All @@ -46,6 +47,7 @@ public WindowsServiceHost(HostEnvironment environment, HostSettings settings, Se
_settings = settings;
_serviceHandle = serviceHandle;
_environment = environment;
_configurator = configurator;

CanPauseAndContinue = settings.CanPauseAndContinue;
CanShutdown = settings.CanShutdown;
Expand Down Expand Up @@ -120,6 +122,9 @@ protected override void OnStart(string[] args)

_log.DebugFormat("[Topshelf] Arguments: {0}", string.Join(",", args));

string startArgs = string.Join(" ", args);
_configurator.ApplyCommandLine(startArgs);

if (!_serviceHandle.Start(this))
throw new TopshelfException("The service did not start successfully (returned false).");

Expand Down

0 comments on commit 922fbff

Please sign in to comment.