diff --git a/NzbDrone.Common/HostController.cs b/NzbDrone.Common/HostController.cs index dc12d724e..c89898ef2 100644 --- a/NzbDrone.Common/HostController.cs +++ b/NzbDrone.Common/HostController.cs @@ -1,48 +1,46 @@ using System; -using System.Linq; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.Owin.Hosting; using NLog; using Nancy.Bootstrapper; -using Nancy.Hosting.Self; +using Nancy.Owin; +using Owin; namespace NzbDrone.Common { public interface IHostController { - bool ServerStarted { get; } string AppUrl { get; } void StartServer(); void RestartServer(); void StopServer(); } - public class HostController : IHostController + + public class OwinHostController : IHostController { private readonly ConfigFileProvider _configFileProvider; - private readonly SecurityProvider _securityProvider; private readonly INancyBootstrapper _bootstrapper; private readonly Logger _logger; - private NancyHost _host; + private IDisposable _host; - - public bool ServerStarted { get; private set; } - - public HostController(ConfigFileProvider configFileProvider, SecurityProvider securityProvider, INancyBootstrapper bootstrapper, Logger logger) + public OwinHostController(ConfigFileProvider configFileProvider, INancyBootstrapper bootstrapper, Logger logger) { _configFileProvider = configFileProvider; - _securityProvider = securityProvider; _bootstrapper = bootstrapper; _logger = logger; } public void StartServer() { - if (_securityProvider.IsNzbDroneUrlRegistered()) - _host = new NancyHost(new Uri(AppUrl), _bootstrapper); + _host = WebApplication.Start(AppUrl, builder => RunNancy(builder, _bootstrapper)); + } - else - _host = new NancyHost(new Uri(AppUrl), _bootstrapper, new HostConfiguration { RewriteLocalhost = false }); - - _host.Start(); + private static IAppBuilder RunNancy(IAppBuilder builder, INancyBootstrapper bootstrapper) + { + var nancyOwinHost = new NancyOwinHost(null, bootstrapper); + return builder.Use((Func, Task>, Func, Task>>)(next => (Func, Task>)nancyOwinHost.Invoke), new object[0]); } public string AppUrl @@ -52,14 +50,9 @@ namespace NzbDrone.Common public void RestartServer() { - _logger.Warn("Attempting to restart server."); - if (_host != null) - { - StopServer(); - } - + StopServer(); StartServer(); } @@ -68,9 +61,11 @@ namespace NzbDrone.Common if (_host == null) return; _logger.Info("Attempting to stop Nancy host"); - _host.Stop(); + _host.Dispose(); _host = null; _logger.Info("Host has stopped"); } + } + } \ No newline at end of file diff --git a/NzbDrone.Common/NzbDrone.Common.csproj b/NzbDrone.Common/NzbDrone.Common.csproj index 3d74e8885..a7b36c17c 100644 --- a/NzbDrone.Common/NzbDrone.Common.csproj +++ b/NzbDrone.Common/NzbDrone.Common.csproj @@ -59,11 +59,17 @@ ..\packages\DotNetZip.1.9.1.8\lib\net20\Ionic.Zip.dll + + ..\packages\Microsoft.Owin.Host.HttpListener.0.21.0-pre\lib\net40\Microsoft.Owin.Host.HttpListener.dll + + + ..\packages\Microsoft.Owin.Hosting.0.21.0-pre\lib\net40\Microsoft.Owin.Hosting.dll + ..\packages\Nancy.0.16.1\lib\net40\Nancy.dll - - ..\packages\Nancy.Hosting.Self.0.16.1\lib\net40\Nancy.Hosting.Self.dll + + ..\packages\Nancy.Owin.0.16.1\lib\net40\Nancy.Owin.dll False @@ -73,6 +79,9 @@ False ..\packages\NLog.2.0.1.2\lib\net40\NLog.dll + + ..\packages\Owin.1.0\lib\net40\Owin.dll + @@ -172,6 +181,9 @@ + + +