Skip to content

Instantly share code, notes, and snippets.

@bmcdavid
Created January 19, 2017 13:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmcdavid/ddceeb01cbddb3d8b62f70b5ffd4cf53 to your computer and use it in GitHub Desktop.
Save bmcdavid/ddceeb01cbddb3d8b62f70b5ffd4cf53 to your computer and use it in GitHub Desktop.
Demo of adding scoped container dependency resolver.
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;
using StructureMap;
using StructureMap.Pipeline;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AlloyDemoKit.Business.Initialization
{
public interface IScopedDictionary : IDictionary<string, object> { };
public class ScopedDictionary : Dictionary<string, object>, IScopedDictionary { }
[EPiServer.Framework.InitializableModule]
public class AddScopedContainer : EPiServer.Framework.IInitializableHttpModule, IConfigurableModule
{
public static readonly string ScopedContainerKeyInContext = typeof(AddScopedContainer).FullName;
static IContainer _Container;
public void ConfigureContainer(ServiceConfigurationContext context)
{
_Container = context.Container;
// just a test
_Container.Configure(x =>
{
x.For<IScopedDictionary>().LifecycleIs(Lifecycles.Container).Use<ScopedDictionary>();
});
}
public void Initialize(InitializationEngine context)
{
System.Web.Mvc.DependencyResolver.SetResolver(new ScopedDependecyResolver(_Container));
}
public void InitializeHttpEvents(HttpApplication application)
{
application.BeginRequest += (sender, e) =>
{
HttpApplication a = sender as HttpApplication;
HttpContext context = a.Context;
var scoped = _Container.CreateChildContainer();
context.Items[ScopedContainerKeyInContext] = scoped;
// just a test to inject to start page controller
var dict = scoped.GetInstance<IScopedDictionary>();
dict["test"] = "Hello";
};
application.EndRequest += (sender, e) =>
{
HttpApplication a = sender as HttpApplication;
HttpContext context = a.Context;
var scoped = context.Items[ScopedContainerKeyInContext] as IContainer;
scoped?.Dispose(); // dispose the child container
};
}
public void Uninitialize(InitializationEngine context) { }
}
public class ScopedDependecyResolver : System.Web.Mvc.IDependencyResolver
{
IContainer Container;
public ScopedDependecyResolver(IContainer container)
{
Container = container;
}
public object GetService(Type serviceType)
{
try
{
return (HttpContext.Current?.GetScopedContainer() ?? Container).GetInstance(serviceType);
}
catch
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return (HttpContext.Current?.GetScopedContainer() ?? Container).GetAllInstances(serviceType).Cast<object>();
}
catch
{
return null;
}
}
}
public static class ScopedExtensions
{
public static IContainer GetScopedContainer(this HttpContext context)
{
return context?.Items[AddScopedContainer.ScopedContainerKeyInContext] as IContainer;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment