在另一个 AppDomain 中创建类型的实例
我的场景是,我有一个创建 AppDomain 的 .net 应用程序(假设是控制台应用程序)。然后,它需要创建实例并调用该 AppDomain 中类型的方法。每个AppDomain都有一个特定的目录,它的依赖项应该在其中,该目录不在控制台应用程序目录下(甚至不在控制台应用程序目录附近)。这是我的简单代码:
string baseDirectory = "c:\foo"; // <- where AppDomain's dependecies
// set up the app domain
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationName = DateTime.Now.ToString("hh:MM:ss:ffff");
setup.ApplicationBase = baseDirectory;
setup.PrivateBinPath = baseDirectory;
// create app domain
AppDomain domain = AppDomain.CreateDomain(
name,
AppDomain.CurrentDomain.Evidence,
setup );
// instantiate Type from an assembly in that AppDomain
ObjectHandle handle = domain.CreateInstanceFrom(
"SampleClassLibrary.dll", // <- exists in "c:\foo"
"SomeClassInTheDll" );
对 CreateInstanceFrom 的调用导致 FileNotFoundExcepotion。 FusionLog显示它搜索的目录是控制台应用程序目录。它不包括从 AppDomain 设置的搜索文件夹 - 在“baseDirecory”变量中。
我做错了什么?是否有另一种方法可以执行位于另一个 AppDomain 中的代码?
谢谢...
My scenario is that I have a .net application (let's say a Console App) that creates AppDomains. It then needs to create instances and call methods on Types that are in that AppDomain. Each AppDomain has a specific directory where are it's dependecies should be, which is not under (or even near) the Console Apps directory. Here's my simple code:
string baseDirectory = "c:\foo"; // <- where AppDomain's dependecies
// set up the app domain
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationName = DateTime.Now.ToString("hh:MM:ss:ffff");
setup.ApplicationBase = baseDirectory;
setup.PrivateBinPath = baseDirectory;
// create app domain
AppDomain domain = AppDomain.CreateDomain(
name,
AppDomain.CurrentDomain.Evidence,
setup );
// instantiate Type from an assembly in that AppDomain
ObjectHandle handle = domain.CreateInstanceFrom(
"SampleClassLibrary.dll", // <- exists in "c:\foo"
"SomeClassInTheDll" );
The call to CreateInstanceFrom results in a FileNotFoundExcepotion. The FusionLog shows that the directories it searchedwere the Console applications directories. It did not include search folders that were set from the AppDomain - in the "baseDirecory" variable.
What am I doing wrong? Is there another way to execute code that lives in another AppDomain?
Thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种解决方法是将完整路径传递给 .CreateInstanceFrom 调用:
One workaround would be to pass the full path to the .CreateInstanceFrom call:
我不能说你的代码有什么问题,因为它看起来应该可以工作。然而,当我完成这件事后,我就找了一个帮手来完成这项工作。大致是这样的:
I can't say what's wrong with your code, as it looks like it should work. When I have doen this, however, I've made a helper to do the work. Roughly like this: