Loading assemblies into a separate AppDomain, getting an InvalidCastException

I am trying to load assemblies into a separate application domain, but I ran into a very strange problem. Here is the code:

public static void LoadAssembly(string assemblyPath) { string pathToDll = Assembly.GetCallingAssembly().CodeBase; AppDomainSetup domainSetup = new AppDomainSetup { PrivateBinPath = pathToDll }; AppDomain newDomain = AppDomain.CreateDomain("AssemblyLoader",null,domainSetup); AssemblyLoader loader = (AssemblyLoader)newDomain.CreateInstanceFromAndUnwrap( pathToDll, typeof(AssemblyLoader).FullName); } 

AssemblyLoader is another class in the same assembly as this one, and it inherits MarshalByRef, however, for some strange reason, I get an exception exception every time I try to run this. I even hardcoded the path to the DLL instead of using GetCallingAssembly (). CodeBase yet I get this exception.

I understand that it is difficult to answer such a question without seeing it and not receiving additional information, but maybe someone has faced a similar situation and will know the general “gotchas” and what I should look for.

EDIT: The reason I don't want to download it directly is because it is just part of the code. The ultimate goal is that this class will have a method that loads assemblies, gets their GUID and some other information about them, and stores them in the database for the project I'm working on. Therefore, if I upload this assembly to a separate application domain, I can upload others there, and then unload the application domain. It makes no sense to download all these assemblies throughout the entire application if I only need this data.

+6
c #
source share
4 answers

(EDIT: after reading this exception, completely changing the answer)

It seems the problem is calling CreateInstanceFromAndUnwrap, which uses the LoadFrom semantics for 'pathToDll'. Suzanne Cook described in detail a possible anchor point in her blog, where your original AppDomain tries to call Load ("SomeAssembly, [...]"), as against "LoadFrom" ("pathToDll") when trying to resolve this type.

Her advice was to bind the AssemblyResolve event in the current domain to make the correct LoadFrom to get the type. A little targeted googling raises a possible solution to the problem based on Susanne's suggestion.

+2
source share

I do not believe that PrivateBinPath configuration is necessary, in addition, you do not need to use the path to the DLL, but rather the full assembly name for the first parameter; try:

 AssemblyLoader loader = (AssemblyLoader)newDomain.CreateInstanceFromAndUnwrap( typeof(AssemblyLoader).Assembly.FullName, typeof(AssemblyLoader).FullName); 
0
source share

There is a lot of good information about what you are trying to do here: How to load the .NET assembly for reflection operations and subsequently unload it?

0
source share

Check this article.

Using the code in this article, I got an object with a cross domain. I digress a bit from generics and you have three builds. (that is, 1 that defines the interface, 1 that defines the implementation of the plug-in, and the main program that tells the universal what to load.) The code for the source articles is easily tracked.

0
source share

All Articles