Problem loading assemblies in another domain

F

Fredo

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

Fredo

Nevermind. The problem had to do with the main assembly resolved. I didn't
spot the calls because of some other weirdness going on. Anyway, it's all
fixed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top