【发布时间】:2016-09-08 05:15:56
【问题描述】:
在 MVC 控制器中,我使用 AssemblyLoadContext.Default.LoadFromAssemblyPath(pathToDll); 加载程序集。我想在运行时删除或替换给定的 .dll 文件。这是不可能的,因为文件没有被释放。有没有办法处理 .dll 文件?有使用AppDomain 类的解决方案,该类在asp.net core 中不可用。
背景: 用户可以上传包含给定接口实现的自定义 .dll 文件。用户还应该能够替换他的文件。我在控制器中使用以下代码来访问实现:
var conventions = new ConventionBuilder();
conventions
.ForTypesDerivedFrom<IPluginContract>()
.Export<IPluginContract>()
.Shared();
var configuration = new ContainerConfiguration().WithAssembliesInPath(path, conventions);
using (var container = configuration.CreateContainer())
{
var plugins = container.GetExports<IPluginContract>();
return plugins;
}
有
public static ContainerConfiguration WithAssembliesInPath(
this ContainerConfiguration configuration,
string path, AttributedModelProvider conventions,
SearchOption searchOption = SearchOption.TopDirectoryOnly)
{
var fileNames = Directory
.GetFiles(path, "*.dll", searchOption);
List<Assembly> assemblies = new List<Assembly>();
foreach (string relativePath in fileNames)
{
Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.GetFullPath(relativePath));
assemblies.Add(assembly);
}
configuration = configuration.WithAssemblies(assemblies, conventions);
return configuration;
}
【问题讨论】:
-
该问题的解决方案是使用
AppDomain类,该类在 asp.net core 中不可用。 -
除了
AppDomain,没有其他方法可以卸载程序集。根据您的用例,您可以尝试采取艰难的方式,启动一个新进程,然后使用 WCF 之类的东西。 -
asp.net core中没有AppDomain的替代品?程序集只能为调用控制器而加载。
标签: c# .net asp.net-core asp.net-core-mvc .net-assembly