简体   繁体   English

CA2202 CA2215 HttpApplication处置基调用

[英]CA2202 CA2215 HttpApplication Dispose base call

I am currently getting the CA2202 ( Do not dispose objects multiple times ) on code that I am analysing. 我目前正在我正在分析的代码上获取CA2202请勿多次放置对象 )。 The warning refers to HttpApplication.Dispose method that I am overriding. 警告是指我重写的HttpApplication.Dispose方法。 I know the IDispose.Dipose method should not be virtual or be overriden but this a result of Microsoft implementing the IDispose.Dispose as virtual in the HttpApplication class and not providing a virtual protected Dispose method. 我知道IDispose.Dipose方法不应是虚拟的或被覆盖,但这是Microsoft在HttpApplication类中实现IDispose.Dispose为虚拟的并且没有提供受保护的虚拟Dispose方法的结果。

The warning appears to be complaining about the calling of the base.Dispose method. 该警告似乎是在抱怨base.Dispose方法的调用。 I am calling the base Dispose method in case the base class needs to close or dispose of any objects. 我正在调用基本Dispose方法,以防基类需要关闭或处理任何对象。 And also suspect that I'll probably end up with the CA2215 ( Dispose methods should call base class dispose ) warning otherwise. 还要怀疑,否则我可能最终会收到CA2215Dispose方法应调用基类dispose )警告。

Does anyone know why I am getting this (CA2202) warning and what to do about it? 有谁知道我为什么收到此警告(CA2202)以及如何处理?

I am tempted to suppress it but the MSDN docs says that it should never be suppressed. 我很想抑制它,但是MSDN文档说它永远不应该被抑制。

The sub class looks like the following (it has been shortened for brevity and you can assume that the _container field has been initialised): 子类如下所示(为简洁起见已被缩短,您可以假定_container字段已初始化):

public class MyHttpApplication : HttpApplication
{
    private bool _disposed;
    private IDisposable _container;

    public sealed override void Dispose()
    {
        if (_disposed)
            return;
        try
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        finally
        {
            //The following lines is what causes the CA2202 code analysis warning
            base.Dispose();
        }

    }

    protected virtual void Dispose(bool disposing)
    {
        if (_disposed)
            return;

        try
        {
            if (disposing && _container != null)
            {
                _container.Dispose();
            }
        }
        finally
        {
            _disposed = true;
        }
    }   
}

It seems there's no way out without suppressing something. 似乎没有抑制的办法是没有出路的。 My solution was to put the call to base.Dispose() inside the Dispose(bool) method then suppress CA2215, for which the MSDN docs say "It is safe to suppress a warning from this rule if the call to base.Dispose occurs at a deeper calling level than the rule checks." 我的解决方案是将对base.Dispose()的调用放在Dispose(bool)方法内,然后禁止CA2215,为此, MSDN文档说:“如果对base.Dispose的调用发生在以下位置,则可以从该规则中禁止警告。比规则检查的呼叫级别更深。”

[SuppressMessage("Microsoft.Usage", "CA2215:Dispose methods should call base class dispose", Justification = "base.Dispose() is called from Dispose(bool)")]
public sealed override void Dispose()
{
    if (_disposed)
        return;
    Dispose(true);
    GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
    if (_disposed)
        return;

    try
    {
        if (disposing)
        {
            base.Dispose();
            if (_container != null)
            {
                _container.Dispose();
            }
        }
    }
    finally
    {
        _disposed = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM