Skip to content

Instantly share code, notes, and snippets.

@NatMarchand
Created May 4, 2018 11:56
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
webformdependencyinjection_autofac1
using System;
using System.Reflection;
using System.Web;
using Autofac.Core.Lifetime;
namespace Autofac.Integration.Web
{
public class AutofacServiceProvider : IServiceProvider
{
private readonly ILifetimeScope _rootContainer;
public AutofacServiceProvider(ILifetimeScope rootContainer)
{
_rootContainer = rootContainer;
}
public object GetService(Type serviceType)
{
if (_rootContainer.IsRegistered(serviceType))
{
return _rootContainer.Resolve(serviceType);
}
return Activator.CreateInstance(serviceType, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, null, null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment