0

我遇到了一个有趣的例外。在我的一台生产服务器上,有时我会遇到以下异常。

System.NullReferenceException: Object reference not set to an instance of an object.
    at System.Xml.DocumentXPathNavigator.get_NameTable()
    at System.Xml.Xsl.Runtime.XmlQueryContext.get_DefaultNameTable()
    at System.Xml.Xsl.Runtime.XmlQueryRuntime..ctor(XmlQueryStaticData data, Object defaultDataSource, XmlResolver dataSources, XsltArgumentList argList, XmlSequenceWriter seqWrt)
    at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
    at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer, Boolean closeWriter)
    at System.Xml.Xsl.XmlILCommand.Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results)
    at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, TextWriter results)
    at sftControls.SearchListLandingPage.getHtml(IXPathNavigable document, String Xsl, String headerLinks)

打开 IL 并查看每个方法表明 DocumentXPathNavigator => this.document 为空。奇怪的是,我无法在本地或我们的测试服务器上复制这个问题。

一点背景知识,这可能是关键……这是一个 Web 应用程序(运行 .Net 3.5),它缓存传递给 XslCompiledTransform.Transform() 的 XmlDocument。我们的缓存通过分布式缓存服务器提供服务。XmlDocument 被序列化和缓存,因此对特定对象的引用不应继续存在。这使我相信,当隐式 XmlDocument -> DocumentXPathNavigator 发生或在 Transform() 运行时删除某处的某些引用时,要么是某些东西明确阻止了 this.document 被设置。无论哪种方式,我都很困惑。

所以,我想我的问题是:

  1. 有没有其他人见过这个?
  2. 有谁知道可能导致它的原因或如何处理它?

谢谢

编辑:
在进一步考虑之后,唯一有意义的是这是一个竞争条件。当应用程序使用对象时,缓存设备以某种方式使对象过期。

编辑/编辑:
.Net 3.5,上面引用的程序集是 2.0

4

2 回答 2

0

我在 XslCompiledTransform 中遇到了一个非常相似的异常:

   System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object.
      at MS.Internal.Xml.Cache.XPathDocumentNavigator.get_NodeType()
      at System.Xml.XPath.XPathNavigator.MoveToNonDescendant()
      at System.Xml.Xsl.Runtime.DescendantMergeIterator.MoveNext(XPathNavigator input)
      at <xsl:template name="SystemPartIncluded">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, IList`1 PartNumber, XPathNavigator Description, IList`1 DescriptionOverride, IList`1 Quantity, IList`1 ExtendedPrice, String LabelWhiteSpace)
      at <xsl:template name="compiler:generated"> (30)(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current, XPathNavigator isBundledWithCanvas, XPathNavigator SoftwareExtendedPrice, XPathNavigator SoftwareUnitPrice, IList`1 PixelNet, IList`1 Revision_Date, IList`1 Catalyst)
      at <xsl:template name="compiler:generated"> (45)(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current, XPathNavigator SoftwareUnitPrice, XPathNavigator HardwareExtendedPrice, XPathNavigator HardwareUnitPrice, XPathNavigator PixelNetUnitPrice, XPathNavigator FusionUnitPrice)
      at <xsl:template match="Quote">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current, Double {urn:schemas-microsoft-com:xslt-debug}position, Double {urn:schemas-microsoft-com:xslt-debug}last)
      at <xsl:template match="/">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current)
      at Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
      at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer)
      at MyApp.clsXML.XslTransformationToHtml(IQEntities DC, Int32 loggedOnUserID, IXPathNavigable input, String xslStylesheetFilePath, XsltArgumentList xslArgs, String XmlRepositoryVersion) in MyApp\App_Data\Static Classes\xml_routines.cs:line 510
      at MyApp.clsQuote.RenderOneQuote(IQEntities DC, Int32 loggedOnUserID, Int32 quoteID, Boolean booPrintFormat, Boolean booDiscount_Prices, Boolean booIs_Super_Administrator, Boolean isHostRep, String strOrganization_Table_Caption, Boolean showBottomLineOnly) in MyApp\App_Data\Static Classes\quote_routines.cs:line 476
      at MyApp.Pages.AJAX.BrowseQuotes.RenderSelectedQuote(Int32 quoteID) in MyApp\Pages\AJAX\BrowseQuotes.aspx.cs:line 66
      at MyApp.Pages.AJAX.BrowseQuotes.ucQuotePicker1_OnQuoteSelectedEvent(Object sender, Int32 quoteID) in b:\DotNet\IQ\WinMetrics.IQ\Pages\AJAX\BrowseQuotes.aspx.cs:line 56
      at System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
      at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
      at System.Web.UI.Page.HandleError(Exception e)
      at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
      at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
      at System.Web.UI.Page.ProcessRequest()
      at System.Web.UI.Page.ProcessRequest(HttpContext context)
      at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

像您一样,我们将 XPathDocument 传递给 Xsl 转换,并且 XPathDocument 正在从分布式缓存中检索。不幸的是,XPathDocument 不可序列化,因此在从分布式缓存中检索后无法使用。我们通过使用 XmlDocument 而不是 XPathDocument 并使用 SerializedXmlDocument 对其进行序列化和反序列化来解决此问题,如下所示:

        public static XmlDocument CachedXmlDoc
        {
            get
            {
                lock (_locker)
                {
                    // maintain the Part XmlDocument in the distributed cache so that it can be reset from any production instance
                    SerializedXmlDocument serializedXmlDoc = Reliable.Application[Global.CACHED_XML_DOC] as SerializedXmlDocument;
                    XmlDocument cachedDoc = (serializedXmlDoc == null) ? null : serializedXmlDoc.XmlDocument;
                    if (cachedDoc == null)
                    {
                        cachedDoc = myController.CreateXmlDoc();
                        Reliable.Application[Global.CACHED_XML_DOC] = new SerializedXmlDocument(cachedDoc);
                    }
                    return cachedDoc;
                }
            }
            set
            {
                lock (_locker)
                {
                    if (value == null)
                    {
                        Reliable.Application[Global.CACHED_XML_DOC] = null;
                    }
                    else
                    {
                        throw new Exception("Tried to set the cached XmlDocument to something other than null.");
                    }
                }
            }
        }
于 2014-01-04T12:41:01.893 回答
0

万一有人有同样的例外。

我们发现当 xmldocument.HasChildNodes 为 false 或 DocumentElement 为 null 时会出现此异常。在查询文档之前验证文档是否包含元素可以解决此问题。

于 2013-04-24T18:17:22.467 回答