当对象超出范围时,Ninject不调用Dispose [英] Ninject doesn't call Dispose on objects when out of scope

查看:256
本文介绍了当对象超出范围时,Ninject不调用Dispose的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶地发现,Ninject创建的至少一个对象在请求结束时不会被处理,当它被定义为InRequestScope



这是我正在处理的对象:



接口:

  public interface IDataContext:IDisposable 
{
MessengerEntities context {get;组; }
}

MessengerEntities是Entity Framework实现的ObjectContext - 我的上下文对象。 p>

然后我创建一个类似这样的具体类:

  public class DataContext: IDataContext 
{
private MessengerEntities _context = new MessengerEntities();
public MessengerEntities context
{
get
{
return _context;
}
set
{
_context = value;
}
}
#region IDisposable会员

public void Dispose()
{
context.Dispose();
}

#endregion
}

那么我有一个这样的Ninject控制器工厂(这是以史蒂夫·桑德森MVC 2书籍为模型):

  public class NinjectControllerFactory: DefaultControllerFactory 
{
//一个Ninject内核是可以提供对象实例的东西
private IKernel kernel = new StandardKernel(new MessengerServices());

// ASP.NET MVC调用这个来获取每个请求的控制器
protected override IController GetControllerInstance(RequestContext requestContext,Type controllerType)
{
if(controllerType == null)
返回null;
return(IController)kernel.Get(controllerType);
}

私有类MessengerServices:NinjectModule
{
public override void Load()
{
绑定< IDataContext>() ; DataContext的>()InRequestScope();
绑定< IArchivesRepository>()。To< ArchivesRepository>()。InRequestScope();
绑定< IMessagesRepository>()。到< MessagesRepository>().InRequestScope();
}
}
}

现在,当我把在DataContext对象中调用context.Dispose()并运行调试器的断点,该代码从未被执行。



因此,证据表明,Ninject不处理的对象,当他们超出范围,但只是创建新的对象,并依赖于垃圾收集器,以在其选择时摆脱他们。



我的问题是我应该担心吗因为我是 - 我会认为Ninject将处理任何实现IDisposable的对象。



更新:我下载了Ninject Mvc扩展MVC 3),这是现在我如何做MvcApplication和绑定,它似乎是处理我的上下文对象。



在global.asax中:

  public class MvcApplication:NinjectHttpApplication 
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute({resource} .axd / {* pathInfo});

routes.MapRoute(
Default,//路由名称
{controller} / {action} / {id},//带参数的URL
new {controller =Home,action =Index,id = UrlParameter.Optional} //参数defaults
);

}

保护覆盖Ninject.IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
返回内核;
}

protected override void OnApplicationStarted()
{
base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}

  public class EFBindingModule:NinjectModule 
{
public override void Load()
{
绑定< IDataContext>()。到<&DataContext的GT;()InRequestScope();
绑定< IArchivesRepository>()。To< ArchivesRepository>()。InRequestScope();
绑定< IMessagesRepository>()。到< MessagesRepository>().InRequestScope();
}
}

其他一切都保持不变。

解决方案

只要请求对象由GC收集,Ninject将处理对象。但通常这需要一些时间。但在请求结束后,有一种方法可以强制提早处理。最好的方法是使用Ninject.Web.MVC http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/ ,而不是实现您自己的ControllerFactory。另一种方法是将应用程序配置为使用OnePerRequestModule。


I was surprised to find that at least one of my objects created by Ninject is not disposed of at the end of the request, when it has been defined to be InRequestScope

Here's the object I'm trying to dispose:

Interface:

public interface IDataContext : IDisposable
{
    MessengerEntities context { get; set; }
}

MessengerEntities is Entity Framework's implementation of ObjectContext -- my context object.

Then I create a concrete class like so:

public class DataContext : IDataContext
{
    private MessengerEntities _context = new MessengerEntities();
    public MessengerEntities context
    {
        get
        {
            return _context;
        }
        set
        {
            _context = value;
        }
    }
    #region IDisposable Members

    public void Dispose()
    {
        context.Dispose();
    }

    #endregion
}

And then I have a Ninject controller factory like so (this is modeled on the Steve Sanderson MVC 2 book):

public class NinjectControllerFactory : DefaultControllerFactory
{
    // a Ninject "kernel" is the thing that can supply object instances
    private IKernel kernel = new StandardKernel(new MessengerServices());

    // ASP.NET MVC calls this to get the controller for each request
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
            return null;
        return (IController)kernel.Get(controllerType);
    }

    private class MessengerServices : NinjectModule
    {
        public override void Load()
        {
            Bind<IDataContext>().To<DataContext>().InRequestScope();
            Bind<IArchivesRepository>().To<ArchivesRepository>().InRequestScope();
            Bind<IMessagesRepository>().To<MessagesRepository>().InRequestScope();
        }
    }
}

Now, when I put a breakpoint at the call to context.Dispose() in the DataContext object and run the debugger, that code never gets executed.

So, the evidence suggests that Ninject does not dispose of objects when they go out of scope, but simply creates new objects and relies on the garbage collector to get rid of them at a time of its choosing.

My question is: should I be concerned about this? Because I am -- I would think Ninject would dispose of any object that implements IDisposable.

UPDATE: I downloaded the Ninject Mvc extensions (for MVC 3) and this is now how I'm doing the MvcApplication and the binding, and it does seem to be disposing of my context object.

In global.asax:

public class MvcApplication : NinjectHttpApplication
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

    protected override Ninject.IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Load(Assembly.GetExecutingAssembly());
        return kernel;
    }

    protected override void OnApplicationStarted()
    {
        base.OnApplicationStarted();
        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }
}

and

public class EFBindingModule : NinjectModule
{
    public override void Load()
    {
        Bind<IDataContext>().To<DataContext>().InRequestScope();
        Bind<IArchivesRepository>().To<ArchivesRepository>().InRequestScope();
        Bind<IMessagesRepository>().To<MessagesRepository>().InRequestScope();
    }
}

Everything else remains the same.

解决方案

Ninject will dispose your objects as soon as the request object is collected by the GC. But normally this takes some time. But there is a way to force early disposal after the request ended. The best way is to use Ninject.Web.MVC http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/ instead of implementing your own ControllerFactory. The other way is to configure your application to use the OnePerRequestModule.

这篇关于当对象超出范围时,Ninject不调用Dispose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆