C# 在 Assembly.Load 时避免文件锁定

标签 c# dll .net-assembly filelock

我的情况是;

我正在设计一个插件应用程序,基于动态加载插件程序集,多个用户可以运行托管在服务器计算机上的应用程序。应用程序在启动时会从我的实时更新服务器自动更新插件程序集。因此插件文件(及其卫星 dll 不应锁定在文件系统上。

 byte[] assemblyBytes = File.ReadAllBytes("asm-path");
 var assembly = Assembly.Load(assemblyBytes);

正如预期的那样,没有锁定 dll 文件。但是如果我加载的 dll 本身具有静态引用 dll 该怎么办?它们现在被锁定在文件系统上。

命名文件,比如说;

  • APP.Exe 是我的基础应用程序;
  • PL1.dll 是我的第一个插件,APP.exe 将其加载到新的 Appdomain
  • PL2.dll 是我的第二个插件,APP.exe 将其加载到新的 Appdomain
  • PL1_S.dll是PL1.dll插件的静态引用dll,加载在PL1 AppDomain中
  • PL_COMMON 是 PL1 和 PL2 插件的静态引用 dll,由 PL1 和 PL2 AppDomain 加载

PL1_S 和 PL_COMMON 也不应该像 PL1 和 PL2 程序集一样锁定在文件中

知道如何解决这个问题吗?

最佳答案

已经内置了执行此操作的功能,例如使用的ASP.Net。请参阅Shadow Copying Assemblies :

Shadow copying enables assemblies that are used in an application domain to be updated without unloading the application domain. This is particularly useful for applications that must be available continuously, such as ASP.NET sites.

所以只需通过 AppDomainSetup.ShadowCopyFiles 打开它创建 AppDomain

关于C# 在 Assembly.Load 时避免文件锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21435290/

相关文章:

c# - TCP是否适合由定期位置更新组成的网络游戏编程?

dll - AzureDevOps 错误找不到类型或命名空间名称 'MyNamespace'(是否缺少 using 指令或程序集引用?)

c# - 创建 C# 到 C++ 桥 : Why do I get a AccessViolationException when calling DLL?

c# - 无法加载文件或程序集 'Microsoft.Office.Interop.Excel'

asp.net动态加载程序集

c# - 使用 C# 自定义 WPF 列表框

C# 如果不再使用,是否需要使用 TCPClient.Close 关闭 TCPClient?

c# - 从 dotnetzip 创建的 zip 文件中提取时出错 - "Windows cannot complete the extraction."

c# - 配置系统无法从 c# com dll 初始化

c# - 从 GAC 动态加载最新的程序集版本