簡體   English   中英

MemoryStream,XmlTextWriter和Warning 4 CA2202:Microsoft.Usage

[英]MemoryStream, XmlTextWriter and Warning 4 CA2202 : Microsoft.Usage

當使用MemoryStreamXmlTextWriter查看某個模式時,Visual Studio 2010 Ultimate中的“ 運行代碼分析”命令會返回警告。

這是警告:

警告7 CA2202:Microsoft.Usage:對象'ms'可以在方法'KinteWritePages.GetXPathDocument(DbConnection)'中多次處理。 為避免生成System.ObjectDisposedException,不應在對象上多次調用Dispose:Lines:421 C:\\ Visual Studio 2010 \\ Projects \\ Songhay.DataAccess.KinteWritePages \\ KinteWritePages.cs 421 Songhay.DataAccess.KinteWritePages

這是形式:

static XPathDocument GetXPathDocument(DbConnection connection)
{
    XPathDocument xpDoc = null;
    var ms = new MemoryStream();
    try
    {
        using(XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8))
        {
            using(DbDataReader reader = CommonReader.GetReader(connection, Resources.KinteRssSql))
            {

                writer.WriteStartDocument();
                writer.WriteStartElement("data");

                do
                {
                    while(reader.Read())
                    {
                        writer.WriteStartElement("item");
                        for(int i = 0; i < reader.FieldCount; i++)
                        {
                            writer.WriteRaw(String.Format("<{0}>{1}</{0}>", reader.GetName(i), reader[i].ToString()));
                        }
                        writer.WriteFullEndElement();
                    }

                } while(reader.NextResult());

                writer.WriteFullEndElement();
                writer.WriteEndDocument();

                writer.Flush();
                ms.Position = 0;

                xpDoc = new XPathDocument(ms);
            }
        }

    }
    finally
    {
        ms.Dispose();
    }

    return xpDoc;
}

為此表單生成了同樣的警告:

XPathDocument xpDoc = null;
using(var ms = new MemoryStream())
{
    using(XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8))
    {
        using(DbDataReader reader = CommonReader.GetReader(connection, Resources.KinteRssSql))
        {
            //...
        }
    }

}

return xpDoc;

順便說一下,以下表格會產生另一個警告:

XPathDocument xpDoc = null;
var ms = new MemoryStream();
using(XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8))
{
    using(DbDataReader reader = CommonReader.GetReader(connection, Resources.KinteRssSql))
    {
        //...
    }
}

return xpDoc;

以上產生警告:

警告7 CA2000:Microsoft.Reliability:在方法“KinteWritePages.GetXPathDocument(DbConnection)”中,對象“ms”未沿所有異常路徑放置。 在對所有引用超出范圍之前,在對象'ms'上調用System.IDisposable.Dispose。 C:\\ Visual Studio 2010 \\ Projects \\ Songhay.DataAccess.KinteWritePages \\ KinteWritePages.cs 383 Songhay.DataAccess.KinteWritePages

除了以下內容,我有哪些選擇?:

  • 禁止警告CA2202。
  • 高舉警告CA2000並希望微軟處理MemoryStream (因為Reflector沒有向我顯示源代碼)。
  • 重寫我的遺留代碼以識別精彩的XDocument和LINQ to XML。

首先,你永遠不應該使用new XmlTextWriter() 自.NET 2.0以來,它已被棄用。 請改用XmlWriter.Create()

其次, ms的賦值應該在using塊中:

using (var ms = new MemoryStream())
{
    using (var writer = XmlWriter.Create(ms))
    {
        // ...
    }
}

我相信警告是正確的。 可以在放置XmlTextWriter時處理MemoryStream ,然后再在“ finally ”塊中處理。

如果這是我的代碼庫,我會壓制它。 代碼分析是為了警告您潛在的問題,只要您(以及團隊中的其他人,包括所有未來的開發人員)都意識到您可能遇到的潛在問題。 這是微軟避免這個問題的using()在我看來,它違背了using()的目的。

在類似的說明中, 這里有一些代碼 ,向您展示如何實際遇到此修復的錯誤。 第一個代碼塊寫入MemoryStream關閉StreamWriter ,然后嘗試在另一個StreamReader讀取它。 不幸的是, StreamWriter上的Dispose()也會關閉MemoryStream 解決方案是從StreamWriter創建StreamReader

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM