diff --git a/readme.md b/readme.md index e0dd0f0..1ff141e 100644 --- a/readme.md +++ b/readme.md @@ -39,18 +39,18 @@ public class Bootstrapper : UnityNancyBootstrapper You can also override the `GetApplicationContainer` method and return a pre-existing container instance, instead of having Nancy create one for you. This is useful if Nancy is co-existing with another application and you want them to share a single container. +It is recommended that a child container of the application's main container is used so that if the application stops and restarts Nancy, the main container is not disposed of. + ```c# protected override IUnityContainer GetApplicationContainer() { // Return application container instance + return parentContainer.CreateChildContainer(); } ``` -If you end up overriding `GetApplicationContainer()`, you will have to manually add the `EnumerableExtension` to your container so that it is able to resolve `IEnumerable` types properly (something that Unity has poor support for). - -```c# -container.AddNewExtension(); -``` +## Changes +V2.0.0 supports Unity 5.11.4 ## Code of Conduct diff --git a/src/Nancy.Bootstrappers.Unity/EnumerableExtension.cs b/src/Nancy.Bootstrappers.Unity/EnumerableExtension.cs index c719ba8..5404c6a 100644 --- a/src/Nancy.Bootstrappers.Unity/EnumerableExtension.cs +++ b/src/Nancy.Bootstrappers.Unity/EnumerableExtension.cs @@ -1,5 +1,5 @@ -using Microsoft.Practices.Unity; -using Microsoft.Practices.Unity.ObjectBuilder; +using Unity.Builder; +using Unity.Extension; namespace Nancy.Bootstrappers.Unity { @@ -8,8 +8,7 @@ public class EnumerableExtension : UnityContainerExtension protected override void Initialize() { // Enumerable strategy - Context.Strategies.AddNew( - UnityBuildStage.TypeMapping); + Context.Strategies.Add(new EnumerableResolutionStrategy(), UnityBuildStage.TypeMapping); } } -} \ No newline at end of file +} diff --git a/src/Nancy.Bootstrappers.Unity/EnumerableResolutionStrategy.cs b/src/Nancy.Bootstrappers.Unity/EnumerableResolutionStrategy.cs index 36d2176..9b02a8e 100644 --- a/src/Nancy.Bootstrappers.Unity/EnumerableResolutionStrategy.cs +++ b/src/Nancy.Bootstrappers.Unity/EnumerableResolutionStrategy.cs @@ -2,9 +2,9 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; -using Microsoft.Practices.ObjectBuilder2; -using Microsoft.Practices.Unity; -using Microsoft.Practices.Unity.Utility; +using Unity; +using Unity.Builder; +using Unity.Strategies; namespace Nancy.Bootstrappers.Unity { @@ -17,7 +17,7 @@ namespace Nancy.Bootstrappers.Unity /// public class EnumerableResolutionStrategy : BuilderStrategy { - private delegate object Resolver(IBuilderContext context); + private delegate object Resolver(BuilderContext context); private static readonly MethodInfo GenericResolveEnumerableMethod = typeof(EnumerableResolutionStrategy).GetMethod("ResolveEnumerable", @@ -31,17 +31,15 @@ public class EnumerableResolutionStrategy : BuilderStrategy /// Do the PreBuildUp stage of construction. This is where the actual work is performed. /// /// Current build context. - public override void PreBuildUp(IBuilderContext context) + public override void PreBuildUp(ref BuilderContext context) { - Guard.ArgumentNotNull(context, "context"); - - if (!IsResolvingIEnumerable(context.BuildKey.Type)) + if (!IsResolvingIEnumerable(context.RegistrationType)) { return; } MethodInfo resolverMethod; - var typeToBuild = GetTypeToBuild(context.BuildKey.Type); + var typeToBuild = GetTypeToBuild(context.RegistrationType); if (IsResolvingLazy(typeToBuild)) { @@ -73,42 +71,39 @@ private static bool IsResolvingLazy(Type type) return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Lazy<>); } - private static object ResolveLazyEnumerable(IBuilderContext context) + private static object ResolveLazyEnumerable(BuilderContext context) { - var container = context.NewBuildUp(); - var typeToBuild = typeof(T); var typeWrapper = typeof(Lazy); - return ResolveAll(container, typeToBuild, typeWrapper).OfType>().ToList();; + return ResolveAll(context, typeToBuild, typeWrapper).OfType>().ToList(); } - private static object ResolveEnumerable(IBuilderContext context) + private static object ResolveEnumerable(BuilderContext context) { - var container = context.NewBuildUp(); - var typeToBuild = typeof(T); - return ResolveAll(container, typeToBuild, typeToBuild).OfType().ToList(); + return ResolveAll(context, typeToBuild, typeToBuild).OfType().ToList(); } - private static IEnumerable ResolveAll(IUnityContainer container, Type type, Type typeWrapper) + private static IEnumerable ResolveAll(BuilderContext context, Type type, Type typeWrapper) { - var names = GetRegisteredNames(container, type); + var names = GetRegisteredNames(context, type); if (type.IsGenericType) { - names = names.Concat(GetRegisteredNames(container, type.GetGenericTypeDefinition())); + names = names.Concat(GetRegisteredNames(context, type.GetGenericTypeDefinition())); } return names.Distinct() .Select(t => t.Name) - .Select(name => container.Resolve(typeWrapper, name)); + .Select(name => context.Resolve(typeWrapper, name)); } - private static IEnumerable GetRegisteredNames(IUnityContainer container, Type type) + private static IEnumerable GetRegisteredNames(BuilderContext context, Type type) { - return container.Registrations.Where(t => t.RegisteredType == type); + return context.Container.Registrations.Where(t => t.RegisteredType == type); } } -} \ No newline at end of file +} + diff --git a/src/Nancy.Bootstrappers.Unity/Nancy.Bootstrappers.Unity.csproj b/src/Nancy.Bootstrappers.Unity/Nancy.Bootstrappers.Unity.csproj index 652571d..b79cc52 100644 --- a/src/Nancy.Bootstrappers.Unity/Nancy.Bootstrappers.Unity.csproj +++ b/src/Nancy.Bootstrappers.Unity/Nancy.Bootstrappers.Unity.csproj @@ -4,12 +4,12 @@ Andreas Håkansson, Steven Robbins and contributors ..\..\dependencies\Nancy\Nancy.ruleset An Unity bootstrapper for the Nancy web framework. - true + true http://nancyfx.org/nancy-nuget.png https://github.com/NancyFx/Nancy/blob/master/license.txt http://nancyfx.org Nancy;Unity - net452 + net472 2.0.0-clinteastwood @@ -18,10 +18,10 @@ - + - + diff --git a/src/Nancy.Bootstrappers.Unity/UnityNancyBootstrapper.cs b/src/Nancy.Bootstrappers.Unity/UnityNancyBootstrapper.cs index b918d76..a4957bd 100644 --- a/src/Nancy.Bootstrappers.Unity/UnityNancyBootstrapper.cs +++ b/src/Nancy.Bootstrappers.Unity/UnityNancyBootstrapper.cs @@ -3,18 +3,21 @@ namespace Nancy.Bootstrappers.Unity using System; using System.Collections.Generic; using Diagnostics; - using Microsoft.Practices.Unity; - using Nancy.Configuration; + using Configuration; using Bootstrapper; + using global::Unity; + using global::Unity.Lifetime; using ViewEngines; /// /// Nancy bootstrapper for the Unity container. /// - public abstract class UnityNancyBootstrapper : NancyBootstrapperWithRequestContainerBase + public abstract class UnityNancyBootstrapper : NancyBootstrapperWithRequestContainerBase, IDisposable { + private bool isDisposing = false; + /// - /// Gets the diagnostics for intialisation + /// Gets the diagnostics for initialisation /// /// An implementation protected override IDiagnostics GetDiagnostics() @@ -250,16 +253,25 @@ protected override IEnumerable GetAllModules(IUnityContainer conta } /// - /// Retreive a specific module instance from the container + /// Retrieve a specific module instance from the container /// /// Container to use /// Type of the module /// An instance protected override INancyModule GetModule(IUnityContainer container, Type moduleType) { - container.RegisterType(typeof(INancyModule), moduleType, new ContainerControlledLifetimeManager()); + return container.Resolve(moduleType) as INancyModule; + } + + public new void Dispose() + { + if (this.isDisposing) + { + return; + } - return container.Resolve(); + this.isDisposing = true; + base.Dispose(); } } } diff --git a/test/Nancy.Bootstrappers.Unity.Tests/Nancy.Bootstrappers.Unity.Tests.csproj b/test/Nancy.Bootstrappers.Unity.Tests/Nancy.Bootstrappers.Unity.Tests.csproj index d24eedc..d70ad05 100644 --- a/test/Nancy.Bootstrappers.Unity.Tests/Nancy.Bootstrappers.Unity.Tests.csproj +++ b/test/Nancy.Bootstrappers.Unity.Tests/Nancy.Bootstrappers.Unity.Tests.csproj @@ -2,8 +2,8 @@ ..\..\dependencies\Nancy\Nancy.ruleset - true - net452 + true + net472 @@ -20,14 +20,17 @@ - - - - - + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - +