3

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. 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:

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:

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

Later this Assembly reference from CreateMethod is used to run a function inside the generated assembly.

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. Looking at the MSDN documentation they show how to use 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.

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. The Run call now takes a Guid as an argument instead of an 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.

dmck
  • 7,801
  • 7
  • 43
  • 79
  • See for example: http://stackoverflow.com/questions/88717/loading-dlls-into-a-separate-appdomain – DeCaf Oct 14 '11 at 19:10

2 Answers2

1

This is one of the major features of an AppDomain! Just go look at the documentation

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Thanks for the link, I've updated the question with some additional details. I'm still not clear if I need to change the signature of CreateMethod to MarshalByRefObject or if I can keep the return type as Assembly – dmck Oct 14 '11 at 19:22
  • In the interest of helping others in the future, I'd like to know: had you previously looked at the documentation for `AppDomain`? – John Saunders Oct 15 '11 at 16:29
1

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. 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. Then have the implementation of Run marshall the results.

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. It's almost like changing the signature in a way...

Kit
  • 20,354
  • 4
  • 60
  • 103
  • Thanks, this was the approach I ended up taking. I had to change the CreateMethod and Run method signatures, but was still able to keep the original design of the code. – dmck Oct 15 '11 at 15:01