在我们的MVC应用程序中,我们主要使用Ninject将依赖项注入控制器。因此,我们的默认生命周期范围是InRequestScope()。我们现在添加了一个使用公共依赖项作为控制器的IHttpModule(即UserService)。问题是HttpModules可以由ASP.NET和IIS池化,并在多个请求中重用。由于注入的依赖项设置为InRequestScope,因此在引用HttpModule中注入的依赖项时,后续请求通常会获得ObjectDisposedException。在注入HttpModule时,如何在注入控制器和InScope()时为UserService指定InRequestScope()。
以下是我们注册的简化版本:
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
public static void Start(){
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
public static void Stop(){
bootstrapper.ShutDown();
}
private static IKernel CreateKernel(){
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
kernel.Bind<IHttpModule>().To<CustomModule>(); // needs a UserService
kernel.Bind<IUserService>().To<UserService>().InRequestScope(); // injected into controllers and the CustomModule
DependencyResolver.SetResolver(new Services.NinjectDependencyResolver(kernel));
return kernel;
}
}
答案 0 :(得分:2)
查看.When()
绑定语法。您可以使用它在特定情况下指定服务的特定范围。这是一个例子:
class Program
{
static void Main(string[] args)
{
var kernel = new StandardKernel();
var scope1 = new object();
var scope2 = new object();
kernel.Bind<IWidget>().To<WidgetA>().InScope(c => scope1);
kernel.Bind<IWidget>().To<WidgetA>().WhenInjectedInto<WidgetController>().InScope(c => scope2);
var service = kernel.Get<GeneralWidgetService>();
service.Print();
service = kernel.Get<GeneralWidgetService>();
service.Print();
var controller = kernel.Get<WidgetController>();
controller.Print();
controller = kernel.Get<WidgetController>();
controller.Print();
// when scope2 changes, WidgetController gets a new widget, while WidgetService continues getting the existing widget
scope2 = new object();
service = kernel.Get<GeneralWidgetService>();
service.Print();
controller = kernel.Get<WidgetController>();
controller.Print();
}
}
public class WidgetController
{
private readonly IWidget _widget;
public WidgetController(IWidget widget)
{
_widget = widget;
}
public void Print()
{
Console.WriteLine("WidgetController ID: " + _widget.ID);
}
}
public class GeneralWidgetService
{
private readonly IWidget _widget;
public GeneralWidgetService(IWidget widget)
{
_widget = widget;
}
public void Print()
{
Console.WriteLine("GeneralWidgetService ID: " + _widget.ID);
}
}
public interface IWidget
{
string ID { get; }
string Name { get; }
}
public class WidgetA : IWidget
{
private readonly string _id = Guid.NewGuid().ToString();
public string ID { get { return _id; } }
public string Name { get { return "AAAAAAAAAAAAAAAAA"; } }
}
输出:
GeneralWidgetService ID:09f61af7-c70d-45fe-834a-6cc94e1e3c40
GeneralWidgetService ID:09f61af7-c70d-45fe-834a-6cc94e1e3c40
WidgetController ID:2c2bf05f-d251-41be-b9e0-224f02839ead
WidgetController ID:2c2bf05f-d251-41be-b9e0-224f02839ead
GeneralWidgetService ID:09f61af7-c70d-45fe-834a-6cc94e1e3c40
WidgetController ID:519a2930-5b71-4cbb-b84e-a1d712ec5398