c# - 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 次,所以我们不确定从哪里开始。
谢谢!
解决方案
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
推荐阅读
- swift - 如何使 JSON 数据持久化以供离线使用(Swift 4)
- angularjs - 在 Django 中,如何使用 angularjs 设置某个请求会话?
- swift - 当我创建一个 segue 以注销时,一个导航控制器突然出现 - Swift
- x86 - 有没有办法确定发生了 SMM 中断?
- spring - 异常连接被拒绝
- c++11 - 具有自定义键类型的 std::unordered_set 允许插入重复键
- java - Android如何限制单个应用实例?
- proof - 在 Idris 中为决策函数构建证明
- linux - 未知错误:在 ubuntu 上执行 Selenium UI 测试用例时,DevToolsActivePort 文件不存在错误
- angular - 迁移旧的角度项目时不兼容的对等依赖关系