指南有关的Dispose()和Ninject [英] Guidelines For Dispose() and Ninject

查看:125
本文介绍了指南有关的Dispose()和Ninject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个WCF服务暴露为这样一种方法:

So, I have a method exposed from a WCF service as such:

public GetAllCommentsResponse GetAllComments(GetAllCommentsRequest request)
{
    var response = new GetAllCommentsResponse();

    using(_unitOfWork)
        try
        {
            Guard.ArgNotNull(request, "request");

            var results = _unitOfWork.CommentRepository.Get(d => d.Id > 0).ToArray();

            //... Do rest of stuff here
        }
        catch (Exception ex)
        {
            response.Success = false;
            response.FailureInformation = ex.Message;
            Logger.LogError("GetAllComments Method Failed", ex);
        }

    return response;
}

我有一个全球性DataUnitOfWork对象(实现IDisposable),其被通过构造函数参数实例化后Ninject当服务电话打进来时调试,如果我用

I have a global DataUnitOfWork object (which implements IDisposable) that gets instantiated by Ninject through a constructor argument when a service call comes in. When debugging, if I use

using(_unitOfWork)

在_unitOfWork对象被走出去的范围,然后得到由Ninject再次打电话后立即处置(尽管它被标记为处理,所以什么也不会发生。)如果没有using语句,Ninject处理处分权。

the _unitOfWork object gets disposed immediately after going out of scope then gets called again by Ninject (although it's been marked as disposed, so nothing happens.) Without the using statement, Ninject handles the disposing.

长话短说,有经验这个一般规则?我已经吓得整个IDisposable的事情一切,我读似乎表明永远使用它,或者用它在某些情况下,不拘一格后,但它总是搞糊涂了。

Long story short, is there a general rule of thumb for this? I've been scared of the whole IDisposable thing after everything I read seems to indicate never to use it, or use it in certain eclectic situations, but it's always confused me.

任何输入AP preciated。

Any input is appreciated.

呵呵,还当我在这里打字呢,到底为什么会出现调用GC.Sup pressFinalize()处理时?如何处置和完成不同?

Oh, also while I'm here typing anyway, why exactly is there a call to GC.SuppressFinalize() when disposing? How do Dispose and Finalize differ?

推荐答案

在CLR文件指出,无论谁创建了一个一次性的对象是负责调用Dispose。在这种情况下,目的是通过Ninject创建。这意味着你应该的没有的显式调用Dispose。

The CLR documentation states that whoever creates a Disposable object is responsible for calling Dispose. In this case the object is created by Ninject. That means you should not call Dispose explicitly.

Ninject部署有另一个范围上比 InTransientScope <一个其他每个一次性对象href="https://web.archive.org/web/20130111064153/http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/">as一旦范围对象,其中所创建的对象是并列由GC 收集。这就是为什么每一个一次性对象应该是绑定 D带的范围,是不是 InTransientScope()。例如。您可以使用 InParentScope()的NamedScope扩展将尽快处置对象为对象被注入到被垃圾收集。

Ninject disposes every Disposable object that has another scope other than InTransientScope as soon as the scope object to which the created object is tied is collected by GC. That's why every Disposable object should be Bindd with a scope that is not InTransientScope(). E.g. you can use InParentScope() from the NamedScope extension which will Dispose the object as soon as the object it is injected into is garbage collected.

这篇关于指南有关的Dispose()和Ninject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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