-
-
Save NatMarchand/25da8383f8fa894a244d813415ba86a3 to your computer and use it in GitHub Desktop.
webformdependencyinjection_autofac1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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