Problem loading assemblies in another domain

Tech-Archive recommends: Speed Up your PC by fixing your registry



I need to create a dialog similar to the AddReferences dialog in VS.NET for
showing the assemblies in the GAC.

Part of what I need to do is get the version #s from the assemblies. There
are more hoop-jumping ways of doing it, but the easiest is to simply load
the assemblies individually and query the info with managed calls.

But of course, I don't want to load every assembly in the GAC into my
AppDomain because it will get huge and I can't unload the assemblies.

So my method of doing it is to create an AppDomain and load an assembly in
that AppDomain, get the info, and then unload the domain when I'm done.

This all worked fine when I did it from a little test app.

The problem I'm running into now is that the main app is plugin based. The
file layout is as follows:

bin\ - Contains main app and all referenced DLLs that aren't plugins
(including assemblies that plugins reference).
bin\Plugins\ - Contains just the plugin DLLs.

Now, there are typically some resolution issues which are handled via the
AppDomain.AssemblyResolve event, and this all works fine.

The problem is, I can't seem to do the same thing with the temporary domain
I'm using.

My code is as follows:

private void LoadGACDataTable()
{
AppDomain domain = AppDomain.CreateDomain("assemblyLoaderDomain");
domain.AssemblyResolve += new
ResolveEventHandler(Domain_AssemblyResolve);
try
{
AssemblyLoader loader = domain.CreateInstanceFromAndUnwrap(
Assembly.GetExecutingAssembly().FullName ,
"ScriptEditor.AssemblyLoader") as AssemblyLoader;

// Gets a list of assemblies in the GAC
List<string> gacItems = GetGACItems();

// LoadDataTable
foreach (string gacItem in gacItems)
{
... loads items into data table ...
}
}
catch(System.Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
finally
{
domain.AssemblyResolve -= new
ResolveEventHandler(Domain_AssemblyResolve);
AppDomain.Unload(domain);
}
}


AssemblyLoader is a class in the plugin and its sole method is simply as
follows:

public void GetAssemblyInfo(string assemblyName, ref string version, ref
string runtime)
{
try
{
Assembly asm = Assembly.LoadFrom(assemblyName);
runtime = asm.ImageRuntimeVersion;
version = asm.GetName().Version.ToString();
}
catch
{
// If it throws, then we just ignore runtime and version info.
}
}

So, essentially, it works like this: The plugin tries to load its own
assembly into the "assemblyLoaderDomain" and then tries to call
AssemblyLoader.GetAssemblyInfo().

The problem is the CreateInstanceFromAndUnwrap() call is throwing a file not
found exception. The AssemblyResolve event for the temporary domain never
gets called, however, so I can't seem to get it to point to the correct
file. The main AppDomain.AssemblyResolve, likewise doesn't get called
either.

Anyone know how I can get this working?

Thanks.


.



Relevant Pages

  • Re: How to find all classes in an assembly having a customAttribute?
    ... and this the preferred way to implement a plugin architecture. ... Remember that if you load assemblies into your current running AppDomain, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to find all classes in an assembly having a customAttribute?
    ... and this the preferred way to implement a plugin architecture. ... > Remember that if you load assemblies into your current running AppDomain, ...
    (microsoft.public.dotnet.languages.csharp)
  • Only .NET 1.0 assemblies in the GAC
    ... I'm experiencing some trouble with the Global Assembly Cache (GAC). ... manually load the 1.1 assemblies in the GAC, but that does not seem to ... I want to load the 1.1 assemblies in the GAC because I am experiencing ... Apparantly this says I am using the .NET framework 1.1, ...
    (microsoft.public.dotnet.framework.setup)
  • Only .NET 1.0 assemblies in the GAC
    ... I'm experiencing some trouble with the Global Assembly Cache (GAC). ... manually load the 1.1 assemblies in the GAC, but that does not seem to ... I want to load the 1.1 assemblies in the GAC because I am experiencing ... Apparantly this says I am using the .NET framework 1.1, ...
    (microsoft.public.dotnet.framework)
  • Re: Using lisp to generate .net business applications
    ... You can load .Net DLLs/Assemblies ... from a known/local directory, OR from the GAC*. ... assemblies, I think it's much cleaner to load them NOT from the ... GAC, and instead load them from "known" paths, such that you ...
    (comp.lang.lisp)