简体   繁体   English

从另一个AppDomain编组程序集

[英]Marshalling Assembly from another AppDomain

Is it possible to hold a reference to an Assembly from another appdomain without having that assembly loaded into the current appdomain? 是否可以从另一个应用程序域保存对程序集的引用,而无需将该程序集加载到当前的应用程序域中?

I'm working on fixing a memory leak in a Windows Service that dynamically generates Assemblies and runs the dynamically generated code. 我正在修复Windows服务中的内存泄漏,该服务动态生成程序集并运行动态生成的代码。 The problem is the generated Assemblies are loaded into the Current app domain and can never be unloaded. 问题是生成的程序集被加载到当前应用程序域中,永远不能卸载。

There is a method in one of the Windows Service libraries that has the follow signature: 其中一个Windows服务库中有一个方法具有以下签名:

public Assembly CreateMethod(ObservableCollection<Field> sourceFields, Field destinationField)

This method creates the code for the assembly and loads it with the CSScript library LoadMethod function: 此方法为程序集创建代码并使用CSScript库LoadMethod函数加载它:

result = CSScript.LoadMethod(scriptFunction.ToString());

Later this Assembly reference from CreateMethod is used to run a function inside the generated assembly. 稍后,来自CreateMethod的此程序集引用用于在生成的程序集内运行函数。

public object Run(Field destinationField, ObservableCollection<LinkField> sourceLinkFields, DataRow mainRow, Assembly script) {

   ...

   var method = script.GetStaticMethodWithArgs("*.a" + Id.ToString().Replace("-", String.Empty), argumentTypes.ToArray());

   return method(arguments.ToArray());
}

I'm wondering if it is possible to load the dynamically generated assembly into another app domain and run them through some type of proxy without having it loaded into the current app domain. 我想知道是否可以将动态生成的程序集加载到另一个应用程序域中,并通过某种类型的代理运行它们,而无需将其加载到当前的应用程序域中。

Edit: 编辑:

I want to know if I can use an Assembly class reference in one AppDomain when the assembly is loaded in another AppDomain. 我想知道当程序集加载到另一个AppDomain中时是否可以在一个AppDomain中使用Assembly类引用。 Looking at the MSDN documentation they show how to use MarshalByRefObject. 查看MSDN文档,它们展示了如何使用MarshalByRefObject。 Basically I am trying to avoid changing the signature to my CreateMethod function, however I may need to change it to return MarshalByRefObject if this is not possible. 基本上我试图避免将签名更改为我的CreateMethod函数,但是如果不可能的话,我可能需要将其更改为返回MarshalByRefObject。

Update: 更新:

I ended up putting the call to CSScript.LoadMethod in the other app domain where I keep a Dictionary I then made CreateMethod return a Guid instead of an Assembly and then I pass this Guid around until the Run call. 我最后在另一个应用程序域中调用了CSScript.LoadMethod,我保留了一个词典,然后让CreateMethod返回一个Guid而不是一个Assembly,然后我传递这个Guid直到Run调用。 The Run call now takes a Guid as an argument instead of an Assembly. Run调用现在将Guid作为参数而不是Assembly。 Inside the Run call I pass the Guid to the other app domain, run the method, and return the result object through a class that inherits MarshalByRefObject. 在Run调用中,我将Guid传递给另一个app域,运行该方法,并通过继承MarshalByRefObject的类返回结果对象。

This is one of the major features of an AppDomain ! 这是AppDomain的主要功能之一! Just go look at the documentation 只需看看文档

If you don't want the dynamic assembly in your main AppDomain, you have to move CreateMethod to another AppDomain, because as soon as you have an instance of Assembly , it's been loaded. 如果您不想在主AppDomain中使用动态程序集,则必须将CreateMethod移动到另一个AppDomain,因为只要有一个Assembly实例,它就会被加载。 In other words, no it is not possible to hold a reference to an assembly in another application domain, only to call into that assembly across application domains. 换句话说,不能在另一个应用程序域中保存对程序集的引用,只能跨应用程序域调用该程序集。

Without changing the signature and a bunch of your code, it seems like you need to move the minimum amount: 1) assembly creation and 2) Run . 在不更改签名和一堆代码的情况下,您似乎需要移动最小量:1)程序集创建和2) Run Then have the implementation of Run marshall the results. 然后执行Run marshall的结果。

As far as CreateMethod I think you want a method in the other assembly to "wrap" CreateMethod and return some sort of token that can be passed to Run . CreateMethod我认为您希望其他程序集中的方法“包装” CreateMethod并返回可以传递给Run某种令牌。 It's almost like changing the signature in a way... 这几乎就像改变签名一样......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM