【问题标题】:Un-Catchable IOException不可捕获的 IOException
【发布时间】:2012-10-18 18:03:35
【问题描述】:

使用 XDocument.Load() 读取 XML 文件的 ASPX.NET 应用程序。有时它会抛出一个 IOException 静态,表明该文件无法打开 b/c 它正在被另一个进程使用。我无法通过打开文件并重新加载站点来随意重新创建它。但更奇怪的是,异常发生在我明确捕获 System.IOException 的 Try-Catch 块中。

这里是堆栈:

异常类型:IOException

线程信息: 线程 ID:18 线程帐户名称:NT AUTHORITY\NETWORK SERVICE 是否冒充:假 堆栈跟踪:在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.FileStream.Init(字符串路径、FileMode 模式、FileAccess 访问、Int32 权限、Boolean useRights、FileShare 共享、Int32 bufferSize、FileOptions 选项、SECURITY_ATTRIBUTES secAttrs、String msgPath、Boolean bFromProxy、Boolean useLongPath) 在 System.IO.FileStream..ctor(字符串路径、FileMode 模式、FileAccess 访问、FileShare 共享、Int32 bufferSize) 在 System.Xml.XmlDownloadManager.GetStream(Uri uri、ICredentials 凭据、IWebProxy 代理、RequestCachePolicy 缓存策略) 在 System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri,字符串角色,ObjectToReturn 类型) 在 System.Xml.XmlReaderSettings.CreateReader(字符串 inputUri,XmlParserContext inputContext) 在 System.Xml.XmlReader.Create(字符串 inputUri,XmlReaderSettings 设置,XmlParserContext inputContext) 在 System.Xml.Linq.XDocument.Load(字符串 uri,LoadOptions 选项) 在 System.Xml.Linq.XDocument.Load(String uri)

在 StatTick.Controls.ChartSlider.getXMLFile(String url) in removedpath\StatTick\Controls\ChartSlider.ascx.cs:line 27 在 StatTick.Controls.ChartSlider.Page_Load(Object sender, EventArgs e) in removedpath\StatTick\Controls\ChartSlider.ascx.cs:line 21

代码如下:

private XDocument getXMLFile(string url)
    {
        XDocument tempDoc;

        t("Looking For XML File");
        int tryCount = 0;
        //string URL = "~/tempCharts/imageList.xml";
        while (tryCount++ < 10)
        {

            try
            {
                tempDoc = XDocument.Load(Server.MapPath(url));
                return tempDoc;
            }
            catch (IOException)
            {
                t("Error accessing XML File, sleeping for 10ms and then trying again\r\nTryCount: " + tryCount.ToString());
                Thread.Sleep(10);
                continue;

            }
        }

        return null;
    }

希望有人能够为我提供一些关于这件事的见解。

谢谢

编辑 好的,这就是我所做的,我将不得不做一些测试以验证它不再抛出感谢您的快速回复!:

while (tryCount <= 10)
        {
            try
            {
                using (FileStream fStream = new FileStream(Server.MapPath(url), FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    XDocument xDoc = XDocument.Load(fStream);

                    foreach (XElement xe in xDoc.Descendants("ImageUrl"))
                    {
                        t("Added: " + xe.Value);
                        tempImageUrlList.Add(xe.Value);
                    }
                    t("Done with Image List!");
                }
                return tempImageUrlList;
            }
            catch (Exception)
            {
                t("Error access XML File, sleeping for 10ms and then trying again\r\nTryCount: " + tryCount.ToString());
                Thread.Sleep(10);
                continue;
            }
        }

【问题讨论】:

    标签: c# asp.net .net


    【解决方案1】:

    改为使用流并使用它加载 XML。您可以确保使用 using 语句关闭底层流。

    using (Stream s = File.OpenRead(Server.MapPath(url)))
    {
            XDocument.Load(s);
    }
    

    或者

    using (FileStream fs= new FileStream(Server.MapPath(url), FileMode.Open,
                                              FileAccess.Read, FileShare.Read))
    {
            XDocument.Load(fs);
    }
    

    ;

    【讨论】:

    • 我正在对此进行一些测试,一旦我得到一些可靠的结果就会标记为答案。谢谢!
    【解决方案2】:

    我认为错误是因为文件的访问权限,所以请授予文件读/写权限,然后尝试读取文件。

    【讨论】:

      猜你喜欢
      • 2015-01-29
      • 2012-11-02
      • 2012-12-19
      • 2018-02-10
      • 2011-03-14
      • 2014-10-09
      • 2017-05-24
      • 2018-04-07
      • 1970-01-01
      相关资源
      最近更新 更多