Skip to content

Instantly share code, notes, and snippets.

@alexeyzimarev
Created April 15, 2015 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexeyzimarev/0209c8dce783bbf347c5 to your computer and use it in GitHub Desktop.
Save alexeyzimarev/0209c8dce783bbf347c5 to your computer and use it in GitHub Desktop.
public class AutofacIocAdapter : IContainerAdapter
{
private readonly IContainer _container;
public AutofacIocAdapter(IContainer container)
{
_container = container;
}
private ILifetimeScope CurrentScope
{
get
{
return HttpContext.Current.Items["AutofacScope"] as ILifetimeScope;
}
}
public T Resolve<T>()
{
return CurrentScope == null ? _container.Resolve<T>() : CurrentScope.Resolve<T>();
}
public T TryResolve<T>()
{
T result;
return CurrentScope == null
? _container.TryResolve(out result) ? result : default(T)
: CurrentScope.TryResolve(out result) ? result : default(T);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment