Skip to content

Commit

Permalink
Updates unity implementation to not use webactivator or a singleton a…
Browse files Browse the repository at this point in the history
…ccessor
  • Loading branch information
Shazwazza committed Jul 26, 2019
1 parent 20f2789 commit 504d361
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 44 deletions.
1 change: 0 additions & 1 deletion src/Our.Umbraco.IoC.Unity/Our.Umbraco.IoC.Unity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="UnityActivator.cs" />
<Compile Include="UnityConfig.cs" />
<Compile Include="UnityResolver.cs" />
<Compile Include="UnityStartup.cs" />
<Compile Include="UnityUmbracoRegister.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/Our.Umbraco.IoC.Unity/Our.Umbraco.IoC.Unity.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<tags>Umbraco IoC DependencyInjection DI Unity</tags>
<dependencies>
<dependency id="Our.Umbraco.IoC" version="[1.0.0,2.0.0)" />
<dependency id="UmbracoCms.Core" version="[7.14.0,8.0.0)" />
<dependency id="UmbracoCms.Core" version="[7.15.0,8.0.0)" />
<dependency id="Unity.AspNet.WebApi" version="[5.11.1,6.0.0]" />
<dependency id="Unity.Mvc" version="[5.11.1,6.0.0]" />
</dependencies>
Expand Down
22 changes: 10 additions & 12 deletions src/Our.Umbraco.IoC.Unity/UnityActivator.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
using Unity.AspNet.Mvc;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using System.Configuration;
using System.Web;
using Unity.AspNet.Mvc;


[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Our.Umbraco.IoC.Unity.UnityActivator), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(Our.Umbraco.IoC.Unity.UnityActivator), "Shutdown")]
[assembly: PreApplicationStartMethod(typeof(Our.Umbraco.IoC.Unity.UnityActivator), "Start")]

namespace Our.Umbraco.IoC.Unity
{
/// <summary>Provides the bootstrapping for integrating Unity with WebApi when it is hosted in ASP.NET</summary>
static class UnityActivator
public static class UnityActivator
{
/// <summary>Integrates Unity when the application starts.</summary>
public static void Start()
{
Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
}
//If this flag exists and it's not 'true' then this container will be disabled.
if (ConfigurationManager.AppSettings["Our.Umbraco.IoC.Unity.Enabled"] != null && ConfigurationManager.AppSettings["Our.Umbraco.IoC.Unity.Enabled"] != "true")
return;

/// <summary>Disposes the Unity container when the application is shut down.</summary>
public static void Shutdown()
{
var container = UnityConfig.GetConfiguredContainer();
container.Dispose();
DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
}
}
}
21 changes: 0 additions & 21 deletions src/Our.Umbraco.IoC.Unity/UnityConfig.cs

This file was deleted.

33 changes: 26 additions & 7 deletions src/Our.Umbraco.IoC.Unity/UnityStartup.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using System.Configuration;
using System.Web.Hosting;
using System.Web.Http;
using System.Web.Mvc;
using Umbraco.Core;
using Umbraco.Web;
using Umbraco.Web.Editors;
using Umbraco.Web.HealthCheck;
using Umbraco.Web.Trees;
using Unity;
using Unity.Injection;


namespace Our.Umbraco.IoC.Unity
{
Expand All @@ -31,11 +28,14 @@ public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, App

public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
////If this flag exists and it's not 'true' then this container will be disabled.
//If this flag exists and it's not 'true' then this container will be disabled.
if (ConfigurationManager.AppSettings["Our.Umbraco.IoC.Unity.Enabled"] != null && ConfigurationManager.AppSettings["Our.Umbraco.IoC.Unity.Enabled"] != "true")
return;

var container = UnityConfig.GetConfiguredContainer();
var container = new UnityContainer();

//register for shutdown
HostingEnvironment.RegisterObject(new UnityShutdown(container));

//register ASP.NET System types
container.RegisterWebTypes();
Expand All @@ -53,6 +53,25 @@ public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, Appl
//set dependency resolvers for both webapi and mvc
GlobalConfiguration.Configuration.DependencyResolver = new global::Unity.AspNet.WebApi.UnityDependencyResolver(container);
DependencyResolver.SetResolver(new global::Unity.AspNet.Mvc.UnityDependencyResolver(container));

}

/// <summary>
/// Disposes the container when the application terminates
/// </summary>
private class UnityShutdown : IRegisteredObject
{
private IUnityContainer _container;

public UnityShutdown(IUnityContainer container)
{
_container = container;
}
public void Stop(bool immediate)
{
_container?.Dispose();
_container = null;
}
}
}
}
2 changes: 1 addition & 1 deletion src/Our.Umbraco.IoC.WebTest/Controllers/TestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public TestController(UmbracoContext umbCtx, BackOfficeUserManager<BackOfficeIde
// GET: Test
public ActionResult Index()
{
return Content($"Hello world. IsFrontEndUmbracoRequest = {_umbCtx.IsFrontEndUmbracoRequest}");
return Content($"Hello world. IsFrontEndUmbracoRequest = {_umbCtx.IsFrontEndUmbracoRequest}. Container Type: {DependencyResolver.Current.GetType()}");
}
}
}
1 change: 0 additions & 1 deletion src/Our.Umbraco.IoC.WebTest/Global.asax
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<%@ Application Inherits="Umbraco.Web.UmbracoApplication" Language="C#" %>

0 comments on commit 504d361

Please sign in to comment.