有谁知道我做错了什么?
我有一个这样的静态类:
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.");
}
}
在我的 MVC 应用程序的 Global.asax.cs 中,我有:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
ApplicationContainer.Create();
RegisterRoutes(RouteTable.Routes);
}
现在关于问题:我如何从容器中解析命名实例?
public class DefaultLogger : ILogger
{
ApplicationContainer.Container().Resolve("Log", typeof(Repository<Log>);// <--- does not work
}
但是当 ApplicationContainer 类不是静态的时,从容器中解析效果很好。我使用 autofac 2.2.4。