From 3a90cc35c2d05212cf7eedbc33c0a816cff5f1d4 Mon Sep 17 00:00:00 2001 From: kayone Date: Sun, 24 Aug 2014 11:24:43 -0700 Subject: [PATCH] Upgraded to Owin 2.1.0 --- src/NzbDrone.Console/NzbDrone.Console.csproj | 14 +-- src/NzbDrone.Console/app.config | 2 + src/NzbDrone.Console/packages.config | 4 +- src/NzbDrone.Host/NzbDrone.Host.csproj | 21 +++-- .../MiddleWare/NzbDroneVersionMiddleWare.cs | 13 ++- src/NzbDrone.Host/Owin/OwinHostController.cs | 71 +++------------- src/NzbDrone.Host/Owin/OwinServiceProvider.cs | 85 +++++++++++++++++-- src/NzbDrone.Host/app.config | 4 + src/NzbDrone.Host/packages.config | 6 +- .../NzbDrone.Integration.Test.csproj | 20 +++-- src/NzbDrone.Integration.Test/app.config | 2 + src/NzbDrone.Integration.Test/packages.config | 6 +- .../NzbDrone.Mono.Test.csproj | 1 + src/NzbDrone.Mono.Test/app.config | 11 +++ .../NzbDrone.Windows.Test.csproj | 1 + src/NzbDrone.Windows.Test/app.config | 11 +++ src/NzbDrone/NzbDrone.csproj | 14 +-- src/NzbDrone/packages.config | 4 +- 18 files changed, 181 insertions(+), 109 deletions(-) create mode 100644 src/NzbDrone.Console/app.config create mode 100644 src/NzbDrone.Integration.Test/app.config create mode 100644 src/NzbDrone.Mono.Test/app.config create mode 100644 src/NzbDrone.Windows.Test/app.config diff --git a/src/NzbDrone.Console/NzbDrone.Console.csproj b/src/NzbDrone.Console/NzbDrone.Console.csproj index 65817efc0..c31cdef62 100644 --- a/src/NzbDrone.Console/NzbDrone.Console.csproj +++ b/src/NzbDrone.Console/NzbDrone.Console.csproj @@ -63,14 +63,16 @@ OnBuildSuccess + + False + ..\packages\Microsoft.Owin.2.1.0\lib\net40\Microsoft.Owin.dll + + + False + ..\packages\Microsoft.Owin.Hosting.2.1.0\lib\net40\Microsoft.Owin.Hosting.dll + - - ..\packages\Microsoft.Owin.1.1.0-beta2\lib\net40\Microsoft.Owin.dll - - - ..\packages\Microsoft.Owin.Hosting.1.1.0-beta2\lib\net40\Microsoft.Owin.Hosting.dll - ..\packages\Newtonsoft.Json.5.0.8\lib\net40\Newtonsoft.Json.dll diff --git a/src/NzbDrone.Console/app.config b/src/NzbDrone.Console/app.config new file mode 100644 index 000000000..a6a2b7fa9 --- /dev/null +++ b/src/NzbDrone.Console/app.config @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/src/NzbDrone.Console/packages.config b/src/NzbDrone.Console/packages.config index abc6822a4..899bdedd0 100644 --- a/src/NzbDrone.Console/packages.config +++ b/src/NzbDrone.Console/packages.config @@ -1,7 +1,7 @@  - - + + diff --git a/src/NzbDrone.Host/NzbDrone.Host.csproj b/src/NzbDrone.Host/NzbDrone.Host.csproj index 98d5d314c..6a1a03752 100644 --- a/src/NzbDrone.Host/NzbDrone.Host.csproj +++ b/src/NzbDrone.Host/NzbDrone.Host.csproj @@ -57,6 +57,18 @@ OnOutputUpdated + + False + ..\packages\Microsoft.Owin.2.1.0\lib\net40\Microsoft.Owin.dll + + + False + ..\packages\Microsoft.Owin.Host.HttpListener.2.1.0\lib\net40\Microsoft.Owin.Host.HttpListener.dll + + + False + ..\packages\Microsoft.Owin.Hosting.2.1.0\lib\net40\Microsoft.Owin.Hosting.dll + False ..\packages\Nancy.0.23.2\lib\net40\Nancy.dll @@ -72,15 +84,6 @@ ..\Libraries\Interop.NetFwTypeLib.dll True - - ..\packages\Microsoft.Owin.1.1.0-beta2\lib\net40\Microsoft.Owin.dll - - - ..\packages\Microsoft.Owin.Host.HttpListener.1.1.0-beta2\lib\net40\Microsoft.Owin.Host.HttpListener.dll - - - ..\packages\Microsoft.Owin.Hosting.1.1.0-beta2\lib\net40\Microsoft.Owin.Hosting.dll - ..\packages\Newtonsoft.Json.5.0.8\lib\net40\Newtonsoft.Json.dll diff --git a/src/NzbDrone.Host/Owin/MiddleWare/NzbDroneVersionMiddleWare.cs b/src/NzbDrone.Host/Owin/MiddleWare/NzbDroneVersionMiddleWare.cs index a7a76644a..33e563db7 100644 --- a/src/NzbDrone.Host/Owin/MiddleWare/NzbDroneVersionMiddleWare.cs +++ b/src/NzbDrone.Host/Owin/MiddleWare/NzbDroneVersionMiddleWare.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Microsoft.Owin; using NzbDrone.Common.EnvironmentInfo; using Owin; @@ -11,7 +12,7 @@ public class NzbDroneVersionMiddleWare : IOwinMiddleWare public void Attach(IAppBuilder appBuilder) { - appBuilder.Use(typeof (AddApplicationVersionHeader)); + appBuilder.Use(typeof(AddApplicationVersionHeader)); } } @@ -21,12 +22,10 @@ public AddApplicationVersionHeader(OwinMiddleware next) : base(next) { } - - public override Task Invoke(OwinRequest request, OwinResponse response) + public override Task Invoke(IOwinContext context) { - response.AddHeader("X-ApplicationVersion", BuildInfo.Version.ToString()); - - return Next.Invoke(request, response); + context.Response.Headers.Add("X-ApplicationVersion", new string[] { BuildInfo.Version.ToString() }); + return Next.Invoke(context); } } } \ No newline at end of file diff --git a/src/NzbDrone.Host/Owin/OwinHostController.cs b/src/NzbDrone.Host/Owin/OwinHostController.cs index f4b967ffb..bfa1e26d2 100644 --- a/src/NzbDrone.Host/Owin/OwinHostController.cs +++ b/src/NzbDrone.Host/Owin/OwinHostController.cs @@ -1,40 +1,30 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Reflection; -using Microsoft.Owin.Hosting; using NLog; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Security; -using NzbDrone.Core.Configuration; using NzbDrone.Host.AccessControl; -using NzbDrone.Host.Owin.MiddleWare; -using Owin; namespace NzbDrone.Host.Owin { public class OwinHostController : IHostController { - private readonly IConfigFileProvider _configFileProvider; - private readonly IEnumerable _owinMiddleWares; + private readonly IOwinAppFactory _owinAppFactory; private readonly IRuntimeInfo _runtimeInfo; private readonly IUrlAclAdapter _urlAclAdapter; private readonly IFirewallAdapter _firewallAdapter; private readonly ISslAdapter _sslAdapter; private readonly Logger _logger; - private IDisposable _host; + private IDisposable _owinApp; - public OwinHostController(IConfigFileProvider configFileProvider, - IEnumerable owinMiddleWares, + public OwinHostController( + IOwinAppFactory owinAppFactory, IRuntimeInfo runtimeInfo, IUrlAclAdapter urlAclAdapter, IFirewallAdapter firewallAdapter, ISslAdapter sslAdapter, Logger logger) { - _configFileProvider = configFileProvider; - _owinMiddleWares = owinMiddleWares; + _owinAppFactory = owinAppFactory; _runtimeInfo = runtimeInfo; _urlAclAdapter = urlAclAdapter; _firewallAdapter = firewallAdapter; @@ -57,60 +47,27 @@ public void StartServer() _urlAclAdapter.ConfigureUrl(); - var options = new StartOptions() - { - ServerFactory = "Microsoft.Owin.Host.HttpListener" - }; - - _urlAclAdapter.Urls.ForEach(options.Urls.Add); - _logger.Info("Listening on the following URLs:"); - foreach (var url in options.Urls) + foreach (var url in _urlAclAdapter.Urls) { _logger.Info(" {0}", url); } - - try - { - _host = WebApp.Start(OwinServiceProviderFactory.Create(), options, BuildApp); - } - catch (TargetInvocationException ex) - { - if (ex.InnerException == null) - { - throw; - } - if (ex.InnerException is HttpListenerException) - { - throw new PortInUseException("Port {0} is already in use, please ensure NzbDrone is not already running.", - ex, - _configFileProvider.Port); - } - - throw ex.InnerException; - } + _owinApp = _owinAppFactory.CreateApp(_urlAclAdapter.Urls); } - private void BuildApp(IAppBuilder appBuilder) - { - appBuilder.Properties["host.AppName"] = "NzbDrone"; - - foreach (var middleWare in _owinMiddleWares.OrderBy(c => c.Order)) - { - _logger.Debug("Attaching {0} to host", middleWare.GetType().Name); - middleWare.Attach(appBuilder); - } - } public void StopServer() { - if (_host == null) return; + if (_owinApp == null) return; - _logger.Info("Attempting to stop Nancy host"); - _host.Dispose(); - _host = null; + _logger.Info("Attempting to stop OWIN host"); + _owinApp.Dispose(); + _owinApp = null; _logger.Info("Host has stopped"); } + + + } } \ No newline at end of file diff --git a/src/NzbDrone.Host/Owin/OwinServiceProvider.cs b/src/NzbDrone.Host/Owin/OwinServiceProvider.cs index cffea738f..c0676cd24 100644 --- a/src/NzbDrone.Host/Owin/OwinServiceProvider.cs +++ b/src/NzbDrone.Host/Owin/OwinServiceProvider.cs @@ -1,14 +1,89 @@ -using Microsoft.Owin.Hosting.Services; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Reflection; +using Microsoft.Owin.Hosting; +using Microsoft.Owin.Hosting.Engine; +using Microsoft.Owin.Hosting.Services; using Microsoft.Owin.Hosting.Tracing; +using NLog; +using NzbDrone.Core.Configuration; +using NzbDrone.Host.Owin.MiddleWare; +using Owin; namespace NzbDrone.Host.Owin { - public static class OwinServiceProviderFactory + public interface IOwinAppFactory { - public static ServiceProvider Create() + IDisposable CreateApp(List urls); + } + + public class OwinAppFactory : IOwinAppFactory + { + private readonly IEnumerable _owinMiddleWares; + private readonly IConfigFileProvider _configFileProvider; + private readonly Logger _logger; + + public OwinAppFactory(IEnumerable owinMiddleWares, IConfigFileProvider configFileProvider, Logger logger) { - var provider = (ServiceProvider)ServicesFactory.Create(); - provider.Add(typeof(ITraceOutputFactory), typeof(OwinTraceOutputFactory)); + _owinMiddleWares = owinMiddleWares; + _configFileProvider = configFileProvider; + _logger = logger; + } + + public IDisposable CreateApp(List urls) + { + var services = CreateServiceFactory(); + var engine = services.GetService(); + + var options = new StartOptions() + { + ServerFactory = "Microsoft.Owin.Host.HttpListener" + }; + + urls.ForEach(options.Urls.Add); + + var context = new StartContext(options) { Startup = BuildApp }; + + + try + { + return engine.Start(context); + } + catch (TargetInvocationException ex) + { + if (ex.InnerException == null) + { + throw; + } + + if (ex.InnerException is HttpListenerException) + { + throw new PortInUseException("Port {0} is already in use, please ensure NzbDrone is not already running.", ex, _configFileProvider.Port); + } + + throw ex.InnerException; + } + } + + + private void BuildApp(IAppBuilder appBuilder) + { + appBuilder.Properties["host.AppName"] = "NzbDrone"; + + foreach (var middleWare in _owinMiddleWares.OrderBy(c => c.Order)) + { + _logger.Debug("Attaching {0} to host", middleWare.GetType().Name); + middleWare.Attach(appBuilder); + } + } + + + private IServiceProvider CreateServiceFactory() + { + var provider = (ServiceProvider)ServicesFactory.Create(); + provider.Add(typeof(ITraceOutputFactory), typeof(OwinTraceOutputFactory)); return provider; } diff --git a/src/NzbDrone.Host/app.config b/src/NzbDrone.Host/app.config index 4c317ad09..0c8db66a2 100644 --- a/src/NzbDrone.Host/app.config +++ b/src/NzbDrone.Host/app.config @@ -28,6 +28,10 @@ + + + + diff --git a/src/NzbDrone.Host/packages.config b/src/NzbDrone.Host/packages.config index 81dc226c1..788ad3919 100644 --- a/src/NzbDrone.Host/packages.config +++ b/src/NzbDrone.Host/packages.config @@ -1,8 +1,8 @@  - - - + + + diff --git a/src/NzbDrone.Integration.Test/NzbDrone.Integration.Test.csproj b/src/NzbDrone.Integration.Test/NzbDrone.Integration.Test.csproj index d51da72c9..bd62c1eac 100644 --- a/src/NzbDrone.Integration.Test/NzbDrone.Integration.Test.csproj +++ b/src/NzbDrone.Integration.Test/NzbDrone.Integration.Test.csproj @@ -38,6 +38,17 @@ 4 + + False + ..\packages\Microsoft.Owin.2.1.0\lib\net40\Microsoft.Owin.dll + + + ..\packages\Microsoft.Owin.Host.HttpListener.2.1.0\lib\net40\Microsoft.Owin.Host.HttpListener.dll + + + False + ..\packages\Microsoft.Owin.Hosting.2.1.0\lib\net40\Microsoft.Owin.Hosting.dll + False ..\packages\Nancy.0.23.2\lib\net40\Nancy.dll @@ -62,15 +73,6 @@ ..\packages\Microsoft.AspNet.SignalR.Client.1.1.3\lib\net40\Microsoft.AspNet.SignalR.Client.dll - - ..\packages\Microsoft.Owin.1.1.0-beta2\lib\net40\Microsoft.Owin.dll - - - ..\packages\Microsoft.Owin.Host.HttpListener.1.1.0-beta2\lib\net40\Microsoft.Owin.Host.HttpListener.dll - - - ..\packages\Microsoft.Owin.Hosting.1.1.0-beta2\lib\net40\Microsoft.Owin.Hosting.dll - ..\packages\Moq.4.0.10827\lib\NET40\Moq.dll diff --git a/src/NzbDrone.Integration.Test/app.config b/src/NzbDrone.Integration.Test/app.config new file mode 100644 index 000000000..a6a2b7fa9 --- /dev/null +++ b/src/NzbDrone.Integration.Test/app.config @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/src/NzbDrone.Integration.Test/packages.config b/src/NzbDrone.Integration.Test/packages.config index ab0dcc448..d702327cf 100644 --- a/src/NzbDrone.Integration.Test/packages.config +++ b/src/NzbDrone.Integration.Test/packages.config @@ -3,9 +3,9 @@ - - - + + + diff --git a/src/NzbDrone.Mono.Test/NzbDrone.Mono.Test.csproj b/src/NzbDrone.Mono.Test/NzbDrone.Mono.Test.csproj index 11b859a9a..eb9bf8115 100644 --- a/src/NzbDrone.Mono.Test/NzbDrone.Mono.Test.csproj +++ b/src/NzbDrone.Mono.Test/NzbDrone.Mono.Test.csproj @@ -72,6 +72,7 @@ + diff --git a/src/NzbDrone.Mono.Test/app.config b/src/NzbDrone.Mono.Test/app.config new file mode 100644 index 000000000..c2022383d --- /dev/null +++ b/src/NzbDrone.Mono.Test/app.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/NzbDrone.Windows.Test/NzbDrone.Windows.Test.csproj b/src/NzbDrone.Windows.Test/NzbDrone.Windows.Test.csproj index 2743876e1..e65483557 100644 --- a/src/NzbDrone.Windows.Test/NzbDrone.Windows.Test.csproj +++ b/src/NzbDrone.Windows.Test/NzbDrone.Windows.Test.csproj @@ -99,6 +99,7 @@ + diff --git a/src/NzbDrone.Windows.Test/app.config b/src/NzbDrone.Windows.Test/app.config new file mode 100644 index 000000000..c2022383d --- /dev/null +++ b/src/NzbDrone.Windows.Test/app.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/NzbDrone/NzbDrone.csproj b/src/NzbDrone/NzbDrone.csproj index 2fd732f12..ed7cd8cf5 100644 --- a/src/NzbDrone/NzbDrone.csproj +++ b/src/NzbDrone/NzbDrone.csproj @@ -63,16 +63,18 @@ OnOutputUpdated + + False + ..\packages\Microsoft.Owin.2.1.0\lib\net40\Microsoft.Owin.dll + + + False + ..\packages\Microsoft.Owin.Hosting.2.1.0\lib\net40\Microsoft.Owin.Hosting.dll + - - ..\packages\Microsoft.Owin.1.1.0-beta2\lib\net40\Microsoft.Owin.dll - - - ..\packages\Microsoft.Owin.Hosting.1.1.0-beta2\lib\net40\Microsoft.Owin.Hosting.dll - ..\packages\Newtonsoft.Json.5.0.8\lib\net40\Newtonsoft.Json.dll diff --git a/src/NzbDrone/packages.config b/src/NzbDrone/packages.config index abc6822a4..899bdedd0 100644 --- a/src/NzbDrone/packages.config +++ b/src/NzbDrone/packages.config @@ -1,7 +1,7 @@  - - + +