【发布时间】:2015-06-30 18:14:36
【问题描述】:
我正在尝试加载插件程序集(放置在
D:\xyz\addin.dll
当我的应用程序在
D:\MyApp\MyApp.exe
这样插件文件不应该被锁定,以便在应用程序运行时可以再次复制它的新版本。 为此,我创建了新的应用程序域并加载了包含 AssembltLoader 类的通用 dll,然后调用 AssemblyLoader 加载插件并获取其中可用的所有类型。 Addin dll 的依赖项已经放在同一个文件夹中。 现在从插件 dll 获取类型工作正常,但是当我
返回程序集.GetTypes();
发生了一件非常奇怪的事情。它执行该行但在该函数退出时抛出异常“d:\xyz\addin.dll”FileNotFound
public class ObjectLoader : IDisposable
{
public Type[] GetAllTypesFromAssembly(string filePath)
{
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
setup.ShadowCopyFiles = "true";
Evidence adevidence = AppDomain.CurrentDomain.Evidence;
AssemblyLoader asemblyLoader = null;
AppDomain appDomain = AppDomain.CreateDomain(filePath, adevidence, setup);
appDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
domains.Add(filePath, appDomain);
object[] parms = { filePath };
BindingFlags bindings = BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public;
try
{
asemblyLoader = (AssemblyLoader)appDomain.CreateInstanceFromAndUnwrap(
setup.ApplicationBase +
"Common.dll", "Common.AssemblyLoader",
true, bindings, null, parms, null, null, null
);
}
catch (Exception ex)
{
throw ex; // new AssemblyLoadFailureException();
}
object types = null;
if (asemblyLoader != null)
{
// On following line i am facing the error which occur on following line but when GetAllTypes exits.
types = asemblyLoader.GetAllTypes();
}
return (Type[])types;
}
}
internal class AssemblyLoader : MarshalByRefObject, IDisposable
{
private Assembly assembly = null;
public object GetAllTypes()
{
BindingFlags flags = BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public;
object types = null;
if (assembly != null)
{
try
{
// following line works fine and loads a type (it contains one class)
types = assembly.GetTypes();
}
catch (Exception)
{
types = new ObjectLoadFailureException();
}
}
else
{
types = new AssemblyNotLoadedException();
}
return types;
}
}
这里是异常详情:
System.IO.FileNotFoundException was unhandled
HResult=-2147024894
Message=Could not load file or assembly 'AddIn, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
Source=mscorlib
FileName=AddIn, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
FusionLog==== Pre-bind state information ===
LOG: DisplayName = AddIn, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///D:/MyApp/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
如果我在 D:\xyz\ 和 D:\MyApp\ 中都复制了我的 addin.dll,则没有错误。我不能在运行时环境中这样做。
【问题讨论】:
-
我添加了解决依赖问题的 AssemblyResolve 事件处理程序,但它会锁定文件,因为这些文件也在当前 AppDomain 中加载
标签: c# dll reflection .net-assembly appdomain