Permalink
Browse files

Get rid of the WindowsServiceLocator.

  • Loading branch information...
1 parent 093ef09 commit 898a2e8a0b655e3ff4d2522598341334457ede85 @lanwin committed May 10, 2012
@@ -44,13 +44,10 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Testing\FakeWindowsServiceFacade.cs" />
- <Compile Include="Testing\FakeWindowsServiceLocator.cs" />
<Compile Include="Infrastructure\IWindowsServiceFacade.cs" />
- <Compile Include="Infrastructure\IWindowsServiceLocator.cs" />
<Compile Include="Scratchpad.cs" />
<Compile Include="Core\NotNullAttribute.cs" />
<Compile Include="Infrastructure\WindowsServiceFacadeReal.cs" />
- <Compile Include="Infrastructure\WindowsServiceLocatorReal.cs" />
<Compile Include="Core\ActionDisposable.cs" />
<Compile Include="Core\ChainCommand.cs" />
<Compile Include="Core\CleanupStrategy.cs" />
@@ -1,13 +1,14 @@
-using System;
+using System.Threading.Tasks;
namespace CommandBasedComponents.Infrastructure
{
public interface IWindowsServiceFacade
{
- bool IsStarted { get; }
- bool IsStopped { get; }
- void StopAndWait(TimeSpan timeout);
- void Kill();
- void Start();
+ bool IsStarted(string name);
+ bool IsStopped(string name);
+ bool Exists(string name);
+ Task<bool> Kill(string name);
+ Task<bool> Start(string name);
+ Task<bool> Stop(string name);
}
}
@@ -1,13 +0,0 @@
-using CommandBasedComponents.Core;
-using CommandBasedComponents.Testing;
-
-namespace CommandBasedComponents.Infrastructure
-{
- public interface IWindowsServiceLocator
- {
- [NotNull]
- FakeWindowsServiceFacade Find(string name);
-
- FakeWindowsServiceFacade TryFind(string name);
- }
-}
@@ -1,54 +1,72 @@
using System;
+using System.Linq;
using System.ServiceProcess;
+using System.Threading.Tasks;
using Microsoft.Win32;
namespace CommandBasedComponents.Infrastructure
{
public class WindowsServiceFacadeReal : IWindowsServiceFacade
{
- readonly ServiceController _controller;
-
- public WindowsServiceFacadeReal(ServiceController controller)
+ bool IWindowsServiceFacade.IsStarted(string name)
{
- _controller = controller;
+ return GetService(name).Status == ServiceControllerStatus.Running;
}
- public bool IsStarted
+ bool IWindowsServiceFacade.IsStopped(string name)
{
- get { return _controller.Status == ServiceControllerStatus.Running; }
+ return GetService(name).Status == ServiceControllerStatus.Stopped;
}
- public bool IsStopped
+ public bool Exists(string name)
{
- get { return _controller.Status == ServiceControllerStatus.Stopped; }
+ return GetService(name) != null;
}
- public void StopAndWait(TimeSpan timeout)
+ public Task<bool> Kill(string name)
{
- _controller.Stop();
- _controller.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
+ return Task.Factory.StartNew(() =>
+ {
+ var key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\services");
+ if(key == null)
+ return true;
+ var controller = GetService(name);
+ key = key.OpenSubKey(controller.ServiceName);
+ if(key == null)
+ return true;
+ var imagePath = (string)key.GetValue("ImagePath", null);
+ // find process & kill
+ return true;
+ });
}
- public void Kill()
+ public Task<bool> Start(string name)
{
- var key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\services");
- if(key == null)
- {
- return;
- }
- key = key.OpenSubKey(_controller.ServiceName);
- if(key == null)
+ return Task.Factory.StartNew(() =>
{
- return;
- }
- var imagePath = (string)key.GetValue("ImagePath", null);
+ var controller = GetService(name);
+ controller.Start();
+ controller.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromMinutes(10));
+ return controller.Status == ServiceControllerStatus.Running;
+ });
+ }
- // find process & kill
+ public Task<bool> Stop(string name)
+ {
+ return Task.Factory.StartNew(() =>
+ {
+ var controller = GetService(name);
+ controller.Stop();
+ controller.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromMinutes(10));
+ return controller.Status == ServiceControllerStatus.Stopped;
+ });
}
- public void Start()
+ static ServiceController GetService(string name)
{
- _controller.Start();
+ return ServiceController
+ .GetServices()
+ .FirstOrDefault(s => s.ServiceName == name);
}
}
}
@@ -1,29 +0,0 @@
-using System;
-using CommandBasedComponents.Core;
-using CommandBasedComponents.Testing;
-
-namespace CommandBasedComponents.Infrastructure
-{
- public class WindowsServiceLocatorReal : IWindowsServiceLocator
- {
- [NotNull]
- public FakeWindowsServiceFacade Find(string name)
- {
- var windowsService = TryFind(name);
- if(windowsService == null)
- {
- throw new InvalidOperationException("Service " + name + " is not installed");
- }
- return windowsService;
- }
-
- public FakeWindowsServiceFacade TryFind(string name)
- {
- throw new NotImplementedException();/*
- return ServiceController.GetServices()
- .Where(s => s.ServiceName.Equals(name, StringComparison.OrdinalIgnoreCase))
- .Select(s => new WindowsServiceFacade(s.n))
- .FirstOrDefault();*/
- }
- }
-}
View
@@ -121,8 +121,8 @@ public static class ServiceKeys
{
public static readonly ContextKey<NugetInstaller> NugetInstaller = new ContextKey<NugetInstaller>();
public static readonly ContextKey<NugetLocator> NugetLocator = new ContextKey<NugetLocator>();
+ public static readonly ContextKey<IWindowsServiceFacade> WindowsServiceFacade = new ContextKey<IWindowsServiceFacade>();
public static readonly ContextKey<WindowsServiceInstaller> WindowsServiceInstaller = new ContextKey<WindowsServiceInstaller>();
- public static readonly ContextKey<IWindowsServiceLocator> WindowsServiceLocator = new ContextKey<IWindowsServiceLocator>();
}
// commands
@@ -196,19 +196,19 @@ public class StopService : ICommand
public void Execute(IContext context)
{
var service = context.Get(PackageKeys.ServiceDescription);
- var locator = context.Get(ServiceKeys.WindowsServiceLocator);
+ var serviceFacade = context.Get(ServiceKeys.WindowsServiceFacade);
- var windowsService = locator.TryFind(service.ServiceName);
-
- if(windowsService == null || windowsService.IsStopped)
+ if(!serviceFacade.Exists(service.ServiceName))
return;
- windowsService.StopAndWait(TimeSpan.FromSeconds(30));
+ serviceFacade.Stop(service.ServiceName)
+ .Wait(30);
- if(windowsService.IsStopped)
+ if(serviceFacade.IsStopped(service.ServiceName))
return;
- windowsService.Kill();
+ serviceFacade.Kill(service.ServiceName)
+ .Wait(30);
}
}
@@ -217,13 +217,13 @@ public class RemoveService : ICommand
public void Execute(IContext context)
{
var service = context.Get(PackageKeys.ServiceDescription);
- var locator = context.Get(ServiceKeys.WindowsServiceLocator);
+ var serviceFacade = context.Get(ServiceKeys.WindowsServiceFacade);
var installer = context.Get(ServiceKeys.WindowsServiceInstaller);
- var windowsService = locator.TryFind(service.ServiceName);
- if(windowsService == null)
+ if(!serviceFacade.Exists(service.ServiceName))
return;
- if(windowsService.IsStopped == false)
+
+ if(!serviceFacade.IsStopped(service.ServiceName))
throw new InvalidOperationException("Service " + service.ServiceName + " need to be stopped to remove it");
installer.Uninstall(service);
@@ -248,11 +248,10 @@ public class InstallSerivce : ICommand
public void Execute(IContext context)
{
var service = context.Get(PackageKeys.ServiceDescription);
- var locator = context.Get(ServiceKeys.WindowsServiceLocator);
+ var serviceFacade = context.Get(ServiceKeys.WindowsServiceFacade);
var installer = context.Get(ServiceKeys.WindowsServiceInstaller);
- var windowsService = locator.TryFind(service.ServiceName);
- if(windowsService != null)
+ if(serviceFacade.Exists(service.ServiceName))
return;
installer.Install(service);
@@ -264,14 +263,15 @@ public class StartService : ICommand
public void Execute(IContext context)
{
var service = context.Get(PackageKeys.ServiceDescription);
- var locator = context.Get(ServiceKeys.WindowsServiceLocator);
+ var serviceFacade = context.Get(ServiceKeys.WindowsServiceFacade);
- var windowsService = locator.Find(service.ServiceName);
+ if(!serviceFacade.Exists(service.ServiceName))
+ return;
- if(windowsService.IsStarted)
+ if(serviceFacade.IsStarted(service.ServiceName))
return;
- windowsService.Start();
+ serviceFacade.Stop(service.ServiceName);
}
}
@@ -440,7 +440,7 @@ public void Run()
using(var context = Context.Empty)
{
- context.Put(ServiceKeys.WindowsServiceLocator, new FakeWindowsServiceLocator());
+ context.Put(ServiceKeys.WindowsServiceFacade, new FakeWindowsServiceFacade());
context.Put(ServiceKeys.WindowsServiceInstaller, new WindowsServiceInstaller());
context.Put(ServiceKeys.NugetInstaller, new NugetInstaller());
context.Put(ServiceKeys.NugetLocator, new NugetLocator());
@@ -1,38 +1,60 @@
using System;
+using System.Threading.Tasks;
using CommandBasedComponents.Infrastructure;
namespace CommandBasedComponents.Testing
{
public class FakeWindowsServiceFacade : IWindowsServiceFacade
{
- readonly string _name;
-
- public FakeWindowsServiceFacade(string name)
+ public FakeWindowsServiceFacade()
{
- _name = name;
IsStopped = true;
}
public bool IsStarted { get; set; }
public bool IsStopped { get; set; }
- public void StopAndWait(TimeSpan timeout)
+ bool IWindowsServiceFacade.IsStarted(string name)
{
- IsStopped = true;
- IsStarted = false;
- Console.WriteLine("Service " + _name + " stopped");
+ return IsStarted;
+ }
+
+ bool IWindowsServiceFacade.IsStopped(string name)
+ {
+ return IsStopped;
+ }
+
+ public bool Exists(string name)
+ {
+ return true;
}
- public void Kill()
+ public Task<bool> Kill(string name)
{
- Console.WriteLine("Service " + _name + " killed");
+ Console.WriteLine("Service " + name + " killed");
+ var source = new TaskCompletionSource<bool>();
+ source.SetResult(true);
+ return source.Task;
}
- public void Start()
+ public Task<bool> Start(string name)
{
IsStarted = true;
IsStopped = false;
- Console.WriteLine("Service " + _name + " started");
+ Console.WriteLine("Service " + name + " started");
+ var source = new TaskCompletionSource<bool>();
+ source.SetResult(true);
+ return source.Task;
+ }
+
+ public Task<bool> Stop(string name)
+ {
+ IsStopped = true;
+ IsStarted = false;
+ Console.WriteLine("Service " + name + " stopped");
+ var source = new TaskCompletionSource<bool>();
+ source.SetResult(true);
+ return source.Task;
}
}
}
@@ -1,25 +0,0 @@
-using System;
-using CommandBasedComponents.Core;
-using CommandBasedComponents.Infrastructure;
-
-namespace CommandBasedComponents.Testing
-{
- public class FakeWindowsServiceLocator : IWindowsServiceLocator
- {
- [NotNull]
- public FakeWindowsServiceFacade Find(string name)
- {
- var windowsService = TryFind(name);
- if(windowsService == null)
- {
- throw new InvalidOperationException("Service " + name + " is not installed");
- }
- return windowsService;
- }
-
- public FakeWindowsServiceFacade TryFind(string name)
- {
- return new FakeWindowsServiceFacade(name);
- }
- }
-}

0 comments on commit 898a2e8

Please sign in to comment.