Windows service is half working,

This commit is contained in:
Keivan Beigi 2011-10-12 19:24:30 -07:00
parent 7549c26ffe
commit 6b7923bd52
12 changed files with 92 additions and 23 deletions

View File

@ -1,12 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using FluentAssertions;
using FluentAssertions;
using NUnit.Framework;
using Ninject;
using NzbDrone.Providers;
namespace NzbDrone.App.Test
{
@ -36,5 +30,14 @@ namespace NzbDrone.App.Test
appServer.Should().NotBeNull();
}
[Test]
public void Kernel_should_resolve_same_ApplicationServer_instance()
{
var appServer1 = CentralDispatch.Kernel.Get<ApplicationServer>();
var appServer2 = CentralDispatch.Kernel.Get<ApplicationServer>();
appServer1.Should().BeSameAs(appServer2);
}
}
}

View File

@ -50,7 +50,9 @@
<Reference Include="Moq">
<HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
</Reference>
<Reference Include="Ninject, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL" />
<Reference Include="Ninject">
<HintPath>..\packages\Ninject.2.2.1.4\lib\net40-Full\Ninject.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.5.10.11092\lib\nunit.framework.dll</HintPath>
</Reference>

View File

@ -46,5 +46,16 @@ namespace NzbDrone.App.Test
serviceController.UnInstall();
serviceController.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeFalse();
}
[Test]
[Explicit]
public void UnInstallService()
{
var serviceController = new ServiceProvider();
//Act
serviceController.UnInstall();
serviceController.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeFalse();
}
}
}

View File

@ -4,6 +4,7 @@
<package id="FluentAssertions" version="1.5.0.0" />
<package id="Moq" version="4.0.10827" />
<package id="NBuilder" version="3.0.1" />
<package id="Ninject" version="2.2.1.4" />
<package id="NUnit" version="2.5.10.11092" />
<package id="Unity" version="2.1.505.0" />
</packages>

View File

@ -18,6 +18,8 @@ namespace NzbDrone
static CentralDispatch()
{
_kernel = new StandardKernel();
BindKernel();
InitilizeApp();
}
public static ApplicationMode ApplicationMode { get; set; }
@ -33,6 +35,7 @@ namespace NzbDrone
private static void BindKernel()
{
_kernel = new StandardKernel();
_kernel.Bind<ApplicationServer>().ToSelf().InSingletonScope();
_kernel.Bind<ConfigProvider>().ToSelf().InSingletonScope();
_kernel.Bind<ConsoleProvider>().ToSelf().InSingletonScope();
_kernel.Bind<DebuggerProvider>().ToSelf().InSingletonScope();
@ -42,6 +45,7 @@ namespace NzbDrone
_kernel.Bind<ProcessProvider>().ToSelf().InSingletonScope();
_kernel.Bind<ServiceProvider>().ToSelf().InSingletonScope();
_kernel.Bind<WebClientProvider>().ToSelf().InSingletonScope();
}
private static void InitilizeApp()

View File

@ -53,6 +53,9 @@
<PropertyGroup>
<ApplicationIcon>NzbDrone.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<StartupObject>NzbDrone.NzbDroneConsole</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="Accessibility">
<EmbedInteropTypes>True</EmbedInteropTypes>

View File

@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading;
using NLog;
using Ninject;
using NzbDrone.Model;
@ -14,10 +16,12 @@ namespace NzbDrone
private static readonly Logger Logger = LogManager.GetLogger("Host.Main");
private static void Main(string[] args)
public static void Main(string[] args)
{
try
{
//while (!Debugger.IsAttached) Thread.Sleep(100);
Console.WriteLine("Starting NzbDrone Console. Version " + Assembly.GetExecutingAssembly().GetName().Version);
CentralDispatch.ApplicationMode = GetApplicationMode(args);
@ -29,9 +33,6 @@ namespace NzbDrone
Console.WriteLine(e.ToString());
Logger.Fatal(e.ToString());
}
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
public static ApplicationMode GetApplicationMode(IEnumerable<string> args)

View File

@ -1,18 +1,43 @@
using System.ServiceProcess;
using System;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
using NLog;
using Ninject;
namespace NzbDrone
{
internal class NzbDroneService : ServiceBase
public class NzbDroneService : ServiceBase
{
private static readonly Logger Logger = LogManager.GetLogger("Host.CentralDispatch");
protected override void OnStart(string[] args)
{
CentralDispatch.Kernel.Get<ApplicationServer>().Start();
try
{
while (!Debugger.IsAttached) Thread.Sleep(100);
Debugger.Break();
CentralDispatch.Kernel.Get<ApplicationServer>().Start();
}
catch (Exception e)
{
Logger.Fatal("Failed to start Windows Service", e);
}
}
protected override void OnStop()
{
CentralDispatch.Kernel.Get<ApplicationServer>().Stop();
try
{
CentralDispatch.Kernel.Get<ApplicationServer>().Stop();
}
catch (Exception e)
{
Logger.Fatal("Failed to stop Windows Service", e);
}
}
}
}

View File

@ -1,20 +1,24 @@
using System;
using NLog;
namespace NzbDrone.Providers
{
public class ConsoleProvider
{
private static readonly Logger Logger = LogManager.GetLogger("Host.ConsoleProvider");
public virtual void WaitForClose()
{
while (true)
{
System.Console.ReadLine();
Console.ReadLine();
}
}
public virtual void PrintHelp()
{
System.Console.WriteLine("Help");
Logger.Info("Printing Help");
Console.WriteLine("Help");
}
}
}

View File

@ -20,14 +20,22 @@ namespace NzbDrone.Providers
{
get
{
var dir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory;
var dir = new FileInfo(Environment.CurrentDirectory).Directory;
while (dir.GetDirectories("iisexpress").Length == 0)
{
if (dir.Parent == null) break;
dir = dir.Parent;
}
dir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory;
while (dir.GetDirectories("iisexpress").Length == 0)
{
if (dir.Parent == null) throw new ApplicationException("Can't fine IISExpress folder.");
dir = dir.Parent;
}
return dir.FullName;
}
}

View File

@ -43,6 +43,8 @@ namespace NzbDrone.Providers
serviceInstaller.DisplayName = NzbDroneServiceName;
serviceInstaller.ServiceName = NzbDroneServiceName;
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.Parent = installer;
serviceInstaller.Install(new ListDictionary());
@ -54,7 +56,7 @@ namespace NzbDrone.Providers
{
var serviceInstaller = new ServiceInstaller();
var context = new InstallContext("install.log", null);
var context = new InstallContext("service_uninstall.log", null);
serviceInstaller.Context = context;
serviceInstaller.ServiceName = NzbDroneServiceName;
serviceInstaller.Uninstall(null);

View File

@ -2,13 +2,16 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog;
using NzbDrone.Model;
using NzbDrone.Providers;
namespace NzbDrone
{
class Router
public class Router
{
private static readonly Logger Logger = LogManager.GetLogger("Host.Router");
private readonly ApplicationServer _applicationServer;
private readonly ServiceProvider _serviceProvider;
private readonly ConsoleProvider _consoleProvider;
@ -17,13 +20,15 @@ namespace NzbDrone
{
_applicationServer = applicationServer;
_serviceProvider = serviceProvider;
_consoleProvider = consoleProvider;
_consoleProvider = consoleProvider;
}
public void Route()
{
Logger.Info("Application mode: {0}", CentralDispatch.ApplicationMode);
switch (CentralDispatch.ApplicationMode)
{
case ApplicationMode.Console:
{
_applicationServer.Start();