【问题标题】:web.config issue when multiple user reads the key at the same time多个用户同时读取密钥时的 web.config 问题
【发布时间】:2021-12-24 00:37:15
【问题描述】:

当多个用户同时读取密钥时,我收到此错误, 加载配置文件时出错:

进程无法访问文件文件“web.config”,因为它正在 被另一个进程使用

我在代码中使用如下代码。

var value = ConfigurationManager.GetSection("keyname") as NameValueCollection;

【问题讨论】:

  • 配置配置 = ConfigurationManager.OpenExeConfiguration(string.Empty);调试了解后,此行导致问题

标签: c# asp.net .net web


【解决方案1】:

如果您从 web.config 并发读取,我建议您使用 ReadWriterLock 类。

在你的课堂上使用:

  static ReaderWriterLock rwl = new ReaderWriterLock();
  ....
  rwl.AcquireReaderLock();
  try {
        // It is safe for this thread to read from the shared resource.
        var value = ConfigurationManager.GetSection("keyname") as NameValueCollection;
     }
     finally {
        // Ensure that the lock is released.
        rwl.ReleaseReaderLock();
     }

文档https://docs.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlock?view=net-5.0

【讨论】:

  • 请阅读此内容stackoverflow.com/questions/745384/… 谢谢
  • 从链接中,静态方法是线程安全的,而实例方法不是。由于GetSection 是静态的,它是安全的,并且由于从它返回的对象被分配给一个局部变量,它似乎并没有跨线程共享。似乎更有可能的是该文件是in use by some other process,或Visual Studio
  • 配置配置 = ConfigurationManager.OpenExeConfiguration(string.Empty);此行导致问题
猜你喜欢
  • 2020-02-02
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 2020-09-26
  • 2017-09-25
相关资源
最近更新 更多