首页 > 解决方案 > Web 服务器访问网络驱动器时出现间歇性 System.IO.DirectoryNotFoundException

问题描述

我们有一个运行 hangfire 的 Web 服务器,它间歇性地轮询共享网络驱动器,路径示例如下:

\\SXXXXX\XXXXX$\XXX\ 

它间歇性地抛出一个 System.IO.DirectoryNotFoundException 但是该目录是由同一段代码创建的。

它似乎运行良好 8/10 次,并且在 2 次它抛出异常它实际上删除文件夹,如果它是在异常点之后运行的代码是空的?

这是一个示例代码块:

 var downloadDirectory = "*OMITTED*
        Directory.CreateDirectory(downloadDirectory);
        TestDirectoryCreatedOnNetwork(downloadDirectory);


var dir = new DirectoryInfo(downloadDirectory);

        if (dir.GetFiles().Count() == 0)
        {
            Directory.Delete(downloadDirectory);
            return false;
        }

谷歌搜索让我们想到: Directory.CreateDirectory() 可能会遭受延迟,因此我们添加了以下内容:

private static void TestDirectoryCreatedOnNetwork(string directory)
    {
        int waitCount = 10;
        do
        {
            if (Directory.Exists(downloadDirectory))
            {
                break;
            }

            Thread.Sleep(10000); // sleep 100ms
            waitCount--;
            if (waitCount <= 0)
            {
                throw new Exception("Failed to create directory");
            }
        } while (true);
    }

我们知道代码可以运行,因为它运行了 8/10 次,所以我们不确定从哪里开始。

谢谢!

标签: c#asp.net-mvchangfire

解决方案


As mentioned in the comments this was caused by poor network conditions, we've since moved the folder creation onto the actual web servers local drives and everything is now working


推荐阅读