How to resolve a named instance from a static class?
Does anyone have any idea about what i doing wrong?
I have a such static class:
public static class ApplicationContainer
{
private static ContainerBuilder builder = null;
private static IContainer container = null;
public static void Create()
{
builder = new ContainerBuilder();
builder.RegisterInstance(new Repository<Log>(RepositoryType.Main))
.As<IRepository<Log>>().SingleInstance()
.Named("Log", typeof(Repository<Log>));
container = builder.Build();
}
public static IContainer Container()
{
if (container != null) return container;
throw new Exception("Container is not ready.");
}
}
In Global.asax.cs of my MVC application i have:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
ApplicationContainer.Create();
RegisterRoutes(RouteTable.Routes);
}
And now about problem: how can i to resolve named instance from a container?
public class DefaultLogger : ILogger
{
ApplicationContai开发者_如何学编程ner.Container().Resolve("Log", typeof(Repository<Log>);// <--- does not work
}
But when ApplicationContainer class is not static, resolving from container works very good. I use autofac 2.2.4.
I believe you cannot compile this code since you are missing a closing ')' for your .Resolve(...)
call.
That said, have you looked at the Autofac ASP.Net integration modules?
精彩评论