我正在尝试注册一个名为“BigEye.Business”的 dll,并将该文件引用到 mvc 3 项目。但是当我试图在那个 dll 中获取一个对象的实例时,autofac 说它没有注册。这是我注册和解析对象的方式:
private static IContainer SetDIContainer()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<UserAuthManager>().As<IUserAuth>().InstancePerHttpRequest();
builder.RegisterType<SessionManager>().As<ISession>().InstancePerHttpRequest();
//Here is the code to register that dll
var services = Assembly.Load("BigEye.Business");
builder.RegisterAssemblyTypes(services);
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
return container;
}
public static T GetInstance<T>()
{
IContainer container = SetDIContainer();
using(var httpRequestScope = container.BeginLifetimeScope("httpRequest"))
{
return httpRequestScope.Resolve<T>();
}
}
当我调用 GetInstance 函数时,我得到“请求的服务 'BigEye.Interfaces.Business.IUser' 尚未注册”。我应该在 Business dll 中注册所有组件吗?有没有办法做到这一点?因为业务中的对象和服务会随时间变化。
谢谢