Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Problem loading assemblies in another domain

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fredo - 16 Mar 2008 20:39 GMT
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.
Fredo - 16 Mar 2008 21:09 GMT
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.

>I need to create a dialog similar to the AddReferences dialog in VS.NET for
>showing the assemblies in the GAC.
[quoted text clipped - 89 lines]
>
> Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.