upgraded nancy bootstrapers to 0.18.0

This commit is contained in:
Keivan Beigi 2013-08-19 18:45:19 -07:00
parent bd4bd47e4e
commit 93b0cf4be9
10 changed files with 114 additions and 85 deletions

View File

@ -44,18 +44,6 @@ namespace NzbDrone.Api
return _tinyIoCContainer;
}
protected override NancyInternalConfiguration InternalConfiguration
{
get
{
var internalConfig = NancyInternalConfiguration.Default;
internalConfig.StatusCodeHandlers.Add(typeof(ErrorHandler));
internalConfig.Serializers.Add(typeof(NancyJsonSerializer));
return internalConfig;
}
}
protected override DiagnosticsConfiguration DiagnosticsConfiguration
{
@ -76,10 +64,5 @@ namespace NzbDrone.Api
return null;
}
}
public void Shutdown()
{
ApplicationContainer.Resolve<IMessageAggregator>().PublishEvent(new ApplicationShutdownRequested());
}
}
}

View File

@ -46,12 +46,13 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.AspNet.SignalR.Core.1.1.3\lib\net40\Microsoft.AspNet.SignalR.Core.dll</HintPath>
</Reference>
<Reference Include="Nancy, Version=0.16.1.0, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="Nancy, Version=0.18.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Nancy.0.16.1\lib\net40\Nancy.dll</HintPath>
<HintPath>..\packages\Nancy.0.18.0\lib\net40\Nancy.dll</HintPath>
</Reference>
<Reference Include="Nancy.Authentication.Basic">
<HintPath>..\packages\Nancy.Authentication.Basic.0.16.1\lib\net40\Nancy.Authentication.Basic.dll</HintPath>
<Reference Include="Nancy.Authentication.Basic, Version=0.18.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Nancy.Authentication.Basic.0.18.0\lib\net40\Nancy.Authentication.Basic.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>

View File

@ -9,25 +9,50 @@ using TinyIoC;
namespace NzbDrone.Api
{
public abstract class TinyIoCNancyBootstrapper : NancyBootstrapperWithRequestContainerBase<TinyIoCContainer>
public class TinyIoCNancyBootstrapper : NancyBootstrapperWithRequestContainerBase<TinyIoCContainer>
{
// <summary>
/// Default assemblies that are ignored for autoregister
/// </summary>
private static readonly IEnumerable<Func<Assembly, bool>> DefaultAutoRegisterIgnoredAssemblies = new Func<Assembly, bool>[]
{
asm => !asm.FullName.StartsWith("Nancy.", StringComparison.InvariantCulture),
};
/// <summary>
/// Gets the assemblies to ignore when autoregistering the application container
/// Return true from the delegate to ignore that particular assembly, returning true
/// does not mean the assembly *will* be included, a false from another delegate will
/// take precedence.
/// </summary>
protected virtual IEnumerable<Func<Assembly, bool>> AutoRegisterIgnoredAssemblies
{
get { return DefaultAutoRegisterIgnoredAssemblies; }
}
/// <summary>
/// Configures the container using AutoRegister followed by registration
/// of default INancyModuleCatalog and IRouteResolver.
/// </summary>
/// <param name="container">Container instance</param>
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
AutoRegister(container, this.AutoRegisterIgnoredAssemblies);
}
/// <summary>
/// Resolve INancyEngine
/// </summary>
/// <returns>INancyEngine implementation</returns>
protected override sealed INancyEngine GetEngineInternal()
{
return ApplicationContainer.Resolve<INancyEngine>();
return this.ApplicationContainer.Resolve<INancyEngine>();
}
/// <summary>
/// Get the moduleKey generator
/// </summary>
/// <returns>IModuleKeyGenerator instance</returns>
protected override sealed IModuleKeyGenerator GetModuleKeyGenerator()
{
return ApplicationContainer.Resolve<IModuleKeyGenerator>();
}
/* protected override IModuleKeyGenerator GetModuleKeyGenerator()
{
return ApplicationContainer.Resolve<IModuleKeyGenerator>();
}*/
/// <summary>
/// Create a default, unconfigured, container
@ -72,7 +97,7 @@ namespace NzbDrone.Api
{
foreach (var collectionTypeRegistration in collectionTypeRegistrationsn)
{
container.RegisterMultiple(collectionTypeRegistration.RegistrationType, collectionTypeRegistration.ImplementationTypes.Distinct());
container.RegisterMultiple(collectionTypeRegistration.RegistrationType, collectionTypeRegistration.ImplementationTypes);
}
}
@ -88,8 +113,8 @@ namespace NzbDrone.Api
container.Register(
typeof(INancyModule),
moduleRegistrationType.ModuleType,
moduleRegistrationType.ModuleKey).
AsSingleton();
moduleRegistrationType.ModuleType.FullName).
AsSingleton();
}
}
@ -114,16 +139,16 @@ namespace NzbDrone.Api
/// <returns>Request container instance</returns>
protected override sealed TinyIoCContainer CreateRequestContainer()
{
return ApplicationContainer.GetChildContainer();
return this.ApplicationContainer.GetChildContainer();
}
/// <summary>
/// Gets the diagnostics for intialisation
/// Gets the diagnostics for initialisation
/// </summary>
/// <returns>IDagnostics implementation</returns>
/// <returns>IDiagnostics implementation</returns>
protected override IDiagnostics GetDiagnostics()
{
return ApplicationContainer.Resolve<IDiagnostics>();
return this.ApplicationContainer.Resolve<IDiagnostics>();
}
/// <summary>
@ -132,7 +157,7 @@ namespace NzbDrone.Api
/// <returns>An <see cref="IEnumerable{T}"/> instance containing <see cref="IApplicationStartup"/> instances. </returns>
protected override IEnumerable<IApplicationStartup> GetApplicationStartupTasks()
{
return ApplicationContainer.ResolveAll<IApplicationStartup>(false);
return this.ApplicationContainer.ResolveAll<IApplicationStartup>(false);
}
/// <summary>
@ -141,7 +166,7 @@ namespace NzbDrone.Api
/// <returns>An <see cref="IEnumerable{T}"/> instance containing <see cref="IApplicationRegistrations"/> instances.</returns>
protected override IEnumerable<IApplicationRegistrations> GetApplicationRegistrationTasks()
{
return ApplicationContainer.ResolveAll<IApplicationRegistrations>(false);
return this.ApplicationContainer.ResolveAll<IApplicationRegistrations>(false);
}
/// <summary>
@ -156,14 +181,16 @@ namespace NzbDrone.Api
}
/// <summary>
/// Retreive a specific module instance from the container by its key
/// Retreive a specific module instance from the container
/// </summary>
/// <param name="container">Container to use</param>
/// <param name="moduleKey">Module key of the module</param>
/// <param name="moduleType">Type of the module</param>
/// <returns>NancyModule instance</returns>
protected override sealed INancyModule GetModuleByKey(TinyIoCContainer container, string moduleKey)
protected override sealed INancyModule GetModule(TinyIoCContainer container, Type moduleType)
{
return container.Resolve<INancyModule>(moduleKey);
container.Register(typeof(INancyModule), moduleType);
return container.Resolve<INancyModule>();
}
/// <summary>
@ -176,7 +203,7 @@ namespace NzbDrone.Api
var whitelist = new Type[] { };
container.AutoRegister(AppDomain.CurrentDomain.GetAssemblies().Where(a => !ignoredAssemblies.Any(ia => ia(a))), t => t.Assembly != assembly || whitelist.Any(wt => wt == t));
container.AutoRegister(AppDomain.CurrentDomain.GetAssemblies().Where(a => !ignoredAssemblies.Any(ia => ia(a))), DuplicateImplementationActions.RegisterMultiple, t => t.Assembly != assembly || whitelist.Any(wt => wt == t));
}
}
}
}

View File

@ -2,8 +2,8 @@
<packages>
<package id="FluentValidation" version="4.0.0.1" targetFramework="net40" />
<package id="Microsoft.AspNet.SignalR.Core" version="1.1.3" targetFramework="net40" />
<package id="Nancy" version="0.16.1" targetFramework="net40" />
<package id="Nancy.Authentication.Basic" version="0.16.1" targetFramework="net40" />
<package id="Nancy" version="0.18.0" targetFramework="net40" />
<package id="Nancy.Authentication.Basic" version="0.18.0" targetFramework="net40" />
<package id="Newtonsoft.Json" version="5.0.6" targetFramework="net40" />
<package id="NLog" version="2.0.1.2" targetFramework="net40" />
<package id="ValueInjecter" version="2.3.3" targetFramework="net40" />

View File

@ -22,7 +22,6 @@
// depending on platform features. If the platform has an appropriate
// #DEFINE then these should be set automatically below.
#define EXPRESSIONS // Platform supports System.Linq.Expressions
#define COMPILED_EXPRESSIONS // Platform supports compiling expressions
#define APPDOMAIN_GETASSEMBLIES // Platform supports getting all assemblies from the AppDomain object
@ -71,12 +70,13 @@ namespace TinyIoC
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using NLog;
#if EXPRESSIONS
using System.Linq.Expressions;
using NLog;
using System.Threading;
#endif
@ -279,7 +279,7 @@ namespace TinyIoC
GC.SuppressFinalize(this);
}
#endregion
#endregion
}
#endif
#endregion
@ -708,6 +708,13 @@ namespace TinyIoC
Fail
}
public enum DuplicateImplementationActions
{
RegisterSingle,
RegisterMultiple,
Fail
}
/// <summary>
/// Resolution settings
/// </summary>
@ -1025,7 +1032,7 @@ namespace TinyIoC
public void AutoRegister()
{
#if APPDOMAIN_GETASSEMBLIES
AutoRegisterInternal(AppDomain.CurrentDomain.GetAssemblies().Where(a => !IsIgnoredAssembly(a)), true, null);
AutoRegisterInternal(AppDomain.CurrentDomain.GetAssemblies().Where(a => !IsIgnoredAssembly(a)), DuplicateImplementationActions.RegisterSingle, null);
#else
AutoRegisterInternal(new Assembly[] {this.GetType().Assembly()}, true, null);
#endif
@ -1042,7 +1049,7 @@ namespace TinyIoC
public void AutoRegister(Func<Type, bool> registrationPredicate)
{
#if APPDOMAIN_GETASSEMBLIES
AutoRegisterInternal(AppDomain.CurrentDomain.GetAssemblies().Where(a => !IsIgnoredAssembly(a)), true, registrationPredicate);
AutoRegisterInternal(AppDomain.CurrentDomain.GetAssemblies().Where(a => !IsIgnoredAssembly(a)), DuplicateImplementationActions.RegisterSingle, registrationPredicate);
#else
AutoRegisterInternal(new Assembly[] { this.GetType().Assembly()}, true, registrationPredicate);
#endif
@ -1051,12 +1058,12 @@ namespace TinyIoC
/// <summary>
/// Attempt to automatically register all non-generic classes and interfaces in the current app domain.
/// </summary>
/// <param name="ignoreDuplicateImplementations">Whether to ignore duplicate implementations of an interface/base class. False=throw an exception</param>
/// <param name="duplicateAction">What action to take when encountering duplicate implementations of an interface/base class.</param>
/// <exception cref="TinyIoCAutoRegistrationException"/>
public void AutoRegister(bool ignoreDuplicateImplementations)
public void AutoRegister(DuplicateImplementationActions duplicateAction)
{
#if APPDOMAIN_GETASSEMBLIES
AutoRegisterInternal(AppDomain.CurrentDomain.GetAssemblies().Where(a => !IsIgnoredAssembly(a)), ignoreDuplicateImplementations, null);
AutoRegisterInternal(AppDomain.CurrentDomain.GetAssemblies().Where(a => !IsIgnoredAssembly(a)), duplicateAction, null);
#else
AutoRegisterInternal(new Assembly[] { this.GetType().Assembly() }, ignoreDuplicateImplementations, null);
#endif
@ -1066,13 +1073,13 @@ namespace TinyIoC
/// Attempt to automatically register all non-generic classes and interfaces in the current app domain.
/// Types will only be registered if they pass the supplied registration predicate.
/// </summary>
/// <param name="ignoreDuplicateImplementations">Whether to ignore duplicate implementations of an interface/base class. False=throw an exception</param>
/// <param name="duplicateAction">What action to take when encountering duplicate implementations of an interface/base class.</param>
/// <param name="registrationPredicate">Predicate to determine if a particular type should be registered</param>
/// <exception cref="TinyIoCAutoRegistrationException"/>
public void AutoRegister(bool ignoreDuplicateImplementations, Func<Type, bool> registrationPredicate)
public void AutoRegister(DuplicateImplementationActions duplicateAction, Func<Type, bool> registrationPredicate)
{
#if APPDOMAIN_GETASSEMBLIES
AutoRegisterInternal(AppDomain.CurrentDomain.GetAssemblies().Where(a => !IsIgnoredAssembly(a)), ignoreDuplicateImplementations, registrationPredicate);
AutoRegisterInternal(AppDomain.CurrentDomain.GetAssemblies().Where(a => !IsIgnoredAssembly(a)), duplicateAction, registrationPredicate);
#else
AutoRegisterInternal(new Assembly[] { this.GetType().Assembly() }, ignoreDuplicateImplementations, registrationPredicate);
#endif
@ -1087,7 +1094,7 @@ namespace TinyIoC
/// <param name="assemblies">Assemblies to process</param>
public void AutoRegister(IEnumerable<Assembly> assemblies)
{
AutoRegisterInternal(assemblies, true, null);
AutoRegisterInternal(assemblies, DuplicateImplementationActions.RegisterSingle, null);
}
/// <summary>
@ -1101,18 +1108,18 @@ namespace TinyIoC
/// <param name="registrationPredicate">Predicate to determine if a particular type should be registered</param>
public void AutoRegister(IEnumerable<Assembly> assemblies, Func<Type, bool> registrationPredicate)
{
AutoRegisterInternal(assemblies, true, registrationPredicate);
AutoRegisterInternal(assemblies, DuplicateImplementationActions.RegisterSingle, registrationPredicate);
}
/// <summary>
/// Attempt to automatically register all non-generic classes and interfaces in the specified assemblies
/// </summary>
/// <param name="assemblies">Assemblies to process</param>
/// <param name="ignoreDuplicateImplementations">Whether to ignore duplicate implementations of an interface/base class. False=throw an exception</param>
/// <param name="duplicateAction">What action to take when encountering duplicate implementations of an interface/base class.</param>
/// <exception cref="TinyIoCAutoRegistrationException"/>
public void AutoRegister(IEnumerable<Assembly> assemblies, bool ignoreDuplicateImplementations)
public void AutoRegister(IEnumerable<Assembly> assemblies, DuplicateImplementationActions duplicateAction)
{
AutoRegisterInternal(assemblies, ignoreDuplicateImplementations, null);
AutoRegisterInternal(assemblies, duplicateAction, null);
}
/// <summary>
@ -1120,12 +1127,12 @@ namespace TinyIoC
/// Types will only be registered if they pass the supplied registration predicate.
/// </summary>
/// <param name="assemblies">Assemblies to process</param>
/// <param name="ignoreDuplicateImplementations">Whether to ignore duplicate implementations of an interface/base class. False=throw an exception</param>
/// <param name="duplicateAction">What action to take when encountering duplicate implementations of an interface/base class.</param>
/// <param name="registrationPredicate">Predicate to determine if a particular type should be registered</param>
/// <exception cref="TinyIoCAutoRegistrationException"/>
public void AutoRegister(IEnumerable<Assembly> assemblies, bool ignoreDuplicateImplementations, Func<Type, bool> registrationPredicate)
public void AutoRegister(IEnumerable<Assembly> assemblies, DuplicateImplementationActions duplicateAction, Func<Type, bool> registrationPredicate)
{
AutoRegisterInternal(assemblies, ignoreDuplicateImplementations, registrationPredicate);
AutoRegisterInternal(assemblies, duplicateAction, registrationPredicate);
}
/// <summary>
@ -3050,7 +3057,7 @@ namespace TinyIoC
#region Internal Methods
private readonly object _AutoRegisterLock = new object();
private void AutoRegisterInternal(IEnumerable<Assembly> assemblies, bool ignoreDuplicateImplementations, Func<Type, bool> registrationPredicate)
private void AutoRegisterInternal(IEnumerable<Assembly> assemblies, DuplicateImplementationActions duplicateAction, Func<Type, bool> registrationPredicate)
{
lock (_AutoRegisterLock)
{
@ -3083,8 +3090,16 @@ namespace TinyIoC
where localType.IsAssignableFrom(implementationType)
select implementationType;
if (!ignoreDuplicateImplementations && implementations.Count() > 1)
throw new TinyIoCAutoRegistrationException(type, implementations);
if (implementations.Count() > 1)
{
if (duplicateAction == DuplicateImplementationActions.Fail)
throw new TinyIoCAutoRegistrationException(type, implementations);
if (duplicateAction == DuplicateImplementationActions.RegisterMultiple)
{
RegisterMultiple(type, implementations);
}
}
var firstImplementation = implementations.FirstOrDefault();
if (firstImplementation != null)
@ -3815,6 +3830,7 @@ namespace TinyIoC
#endregion
}
}
// reverse shim for WinRT SR changes...

View File

@ -89,12 +89,13 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.Owin.Hosting.1.1.0-beta2\lib\net40\Microsoft.Owin.Hosting.dll</HintPath>
</Reference>
<Reference Include="Nancy">
<HintPath>..\packages\Nancy.0.16.1\lib\net40\Nancy.dll</HintPath>
</Reference>
<Reference Include="Nancy.Owin, Version=0.16.1.0, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="Nancy, Version=0.18.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Nancy.Owin.0.16.1\lib\net40\Nancy.Owin.dll</HintPath>
<HintPath>..\packages\Nancy.0.18.0\lib\net40\Nancy.dll</HintPath>
</Reference>
<Reference Include="Nancy.Owin, Version=0.18.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Nancy.Owin.0.18.0\lib\net40\Nancy.Owin.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>

View File

@ -20,7 +20,7 @@ namespace NzbDrone.Host.Owin.MiddleWare
public void Attach(IAppBuilder appBuilder)
{
var nancyOwinHost = new NancyOwinHost(null, _nancyBootstrapper);
var nancyOwinHost = new NancyOwinHost(null, _nancyBootstrapper, new HostConfiguration());
appBuilder.Use((Func<Func<IDictionary<string, object>, Task>, Func<IDictionary<string, object>, Task>>)(next => (Func<IDictionary<string, object>, Task>)nancyOwinHost.Invoke), new object[0]);
}
}

View File

@ -5,8 +5,8 @@
<package id="Microsoft.Owin" version="1.1.0-beta2" targetFramework="net40" />
<package id="Microsoft.Owin.Host.HttpListener" version="1.1.0-beta2" targetFramework="net40" />
<package id="Microsoft.Owin.Hosting" version="1.1.0-beta2" targetFramework="net40" />
<package id="Nancy" version="0.16.1" targetFramework="net40" />
<package id="Nancy.Owin" version="0.16.1" targetFramework="net40" />
<package id="Nancy" version="0.18.0" targetFramework="net40" />
<package id="Nancy.Owin" version="0.18.0" targetFramework="net40" />
<package id="Newtonsoft.Json" version="5.0.6" targetFramework="net40" />
<package id="NLog" version="2.0.1.2" targetFramework="net40" />
<package id="NLog.Config" version="2.0.1.2" targetFramework="net40" />

View File

@ -58,12 +58,13 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
</Reference>
<Reference Include="Nancy">
<HintPath>..\packages\Nancy.0.16.1\lib\net40\Nancy.dll</HintPath>
</Reference>
<Reference Include="Nancy.Owin, Version=0.16.1.0, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="Nancy, Version=0.18.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Nancy.Owin.0.16.1\lib\net40\Nancy.Owin.dll</HintPath>
<HintPath>..\packages\Nancy.0.18.0\lib\net40\Nancy.dll</HintPath>
</Reference>
<Reference Include="Nancy.Owin, Version=0.18.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Nancy.Owin.0.18.0\lib\net40\Nancy.Owin.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>

View File

@ -7,8 +7,8 @@
<package id="Microsoft.Owin.Host.HttpListener" version="1.1.0-beta2" targetFramework="net40" />
<package id="Microsoft.Owin.Hosting" version="1.1.0-beta2" targetFramework="net40" />
<package id="Moq" version="4.0.10827" targetFramework="net40" />
<package id="Nancy" version="0.16.1" targetFramework="net40" />
<package id="Nancy.Owin" version="0.16.1" targetFramework="net40" />
<package id="Nancy" version="0.18.0" targetFramework="net40" />
<package id="Nancy.Owin" version="0.18.0" targetFramework="net40" />
<package id="Newtonsoft.Json" version="5.0.6" targetFramework="net40" />
<package id="NLog" version="2.0.1.2" targetFramework="net40" />
<package id="NUnit" version="2.6.2" targetFramework="net40" />