.Net - 从 AppDomain 动态加载类型,无需远程代理

发布于 2024-09-11 20:46:04 字数 740 浏览 4 评论 0原文

我已经加载了一个特定的 AppDomain,并且我想从中动态加载一些类型(小菜一碟,对吧?)。问题是 AppDomain 类的所有 CreateInstance 方法都将事物作为远程对象句柄返回。远程代理具有我希望避免的限制,例如:必须具有可序列化的具体类,以及过度急切的垃圾收集,除非使用 LifeTimeService。

我的问题是如何在另一个应用程序域中加载类型而不将其包装在远程代理中?下面是我的代码片段。

AppDomainSetup ads = new AppDomainSetup();
ads.ApplicationBase = Path.GetDirectoryName(_bllAssemblyPath);
ads.PrivateBinPath =  Path.GetDirectoryName(_bllAssemblyPath);
if (File.Exists(_bllAssemblyPath + ".config"))
    ads.ConfigurationFile = _bllAssemblyPath + ".config";
_workerSpace= AppDomain.CreateDomain("worker", new System.Security.Policy.Evidence(AppDomain.CurrentDomain.Evidence), ads );

_bllQueue = _workerSpace.CreateInstanceFrom(_bllAssemblyPath, queueType) as IThumbCapQueue;

I've loaded a specific AppDomain up and I want to load some types dynamically from it (piece of cake right?). The thing is all of the CreateInstance methods of the AppDomain class return things as a remoting object handle. Remoting proxies have limitations that I would like to avoid such as: having to have serializable concrete classes, and over eager garbage collection unless LifeTimeService is used.

My Question is how can I load a type in another app domain without having it wrapped in a remoting proxy? Below is a snippet of my code.

AppDomainSetup ads = new AppDomainSetup();
ads.ApplicationBase = Path.GetDirectoryName(_bllAssemblyPath);
ads.PrivateBinPath =  Path.GetDirectoryName(_bllAssemblyPath);
if (File.Exists(_bllAssemblyPath + ".config"))
    ads.ConfigurationFile = _bllAssemblyPath + ".config";
_workerSpace= AppDomain.CreateDomain("worker", new System.Security.Policy.Evidence(AppDomain.CurrentDomain.Evidence), ads );

_bllQueue = _workerSpace.CreateInstanceFrom(_bllAssemblyPath, queueType) as IThumbCapQueue;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫轩蝶泪 2024-09-18 20:46:04

您始终需要某种类型的代理来在应用程序域之间进行通信,因为 .NET 不允许您直接访问另一个应用程序域中对象的内存。另请注意,示例中的两个 AppDomain 将在同一 Windows 进程中运行。

有一个用于远程处理的新基础架构:RIA 服务,它可能会为您提供您想要的功能。

You will always need some type of proxy to talk between app domains as .NET won't let you directly access the memory of objects in another app domain. Also note that both AppDomains in your example will run within the same Windows Process.

There is a new infrastructure for remoting: RIA Services that might give you the features you want.

厌味 2024-09-18 20:46:04

不要使您感兴趣的对象可远程访问,而是创建一个远程“引导程序”作为远程 AppDomain 的通信通道,并使用它来加载您感兴趣的对象。我正在一个项目上这样做需要反映任意 .NET dll 以从中获取类型信息(我在新的 AppDomain 中执行此操作,因为完成后我想卸载程序集,以便文件不被锁定) - 远程处理填充程序加载程序集,进行反射,收集必要的信息并将其以可序列化对象的形式发送回调用 AppDomain。请参阅此处

Instead of making the objects you are interested in remotable, make a remotable "bootstrapper" that serves as a communication channel to the remote AppDomain, and use it to load up what you're interested in. I'm doing this on a project that requires reflecting arbitrary .NET dlls to get type information from them (I do it in a new AppDomain because when I'm done I want to unload the assembly so the file's not locked) - the remoting shim loads the assembly, does reflection, gathers the necessary information and sends it back to the calling AppDomain in the form of serializable objects. See here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文