lock ExtractToDirectory 只运行一次
此功能需要几秒钟才能在本地运行。它将ZIP文件打开到本地服务器上,然后在ZIP中使用单个文件之前被调用。当不存在提取的文件夹时,它只能运行一次。
internal static void Init(string releaseName)
{
var unpackedFolderDirectory = Settings.Editor.FilesRootFolder + releaseName + "\\";
var dirInfo = new DirectoryInfo(unpackedFolderDirectory);
if (dirInfo.Exists) return;
lock ("UnpackLock")
{
dirInfo.Refresh();
if (dirInfo.Exists) return;
// Unpack all
var bytes = getBytesFromAzure();
using var ms = new MemoryStream(bytes);
using (var archive = new ZipArchive(ms))
{
archive.ExtractToDirectory(unpackedFolderDirectory);
}
}
}
如果我对此功能提出了多个请求,则其中一些返回错误:
The file 'C:\SomeFolder\SomeSubFolder\SomeFile.png' already exists.
On line -> archive.ExtractToDirectory(unpackedFolderDirectory);
我期望Archive.extractTodirectory(unpackedFolderDirectory);
仅执行一次,但似乎运行多次。
我在做什么错?我没有发现一些种族状况吗?
This function takes a few seconds to run locally. It unpacks a zip file onto the local server and is called before serving individual files in the zip. It should only run once if the extracted folder does not exist.
internal static void Init(string releaseName)
{
var unpackedFolderDirectory = Settings.Editor.FilesRootFolder + releaseName + "\\";
var dirInfo = new DirectoryInfo(unpackedFolderDirectory);
if (dirInfo.Exists) return;
lock ("UnpackLock")
{
dirInfo.Refresh();
if (dirInfo.Exists) return;
// Unpack all
var bytes = getBytesFromAzure();
using var ms = new MemoryStream(bytes);
using (var archive = new ZipArchive(ms))
{
archive.ExtractToDirectory(unpackedFolderDirectory);
}
}
}
If I make multiple requests to this function, some of them return the error:
The file 'C:\SomeFolder\SomeSubFolder\SomeFile.png' already exists.
On line -> archive.ExtractToDirectory(unpackedFolderDirectory);
I am expecting the archive.ExtractToDirectory(unpackedFolderDirectory);
to only execute once, but it appears to be running multiple times.
What am I doing wrong? Is there some race condition here I'm not spotting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论