DCSIMG
How to load an assembly at runtime from specific location - Rotem Bloom's Blog

Rotem Bloom's Blog

Share knowledge on .NET and web development

News

About Me

Glad to help and share knowledge on .NET and also huge fan of Dream Theater.

Contact me via messenger

View Rotem Bloom's profile on LinkedIn

Dream Theater Pics

Blogs I Read

How to load an assembly at runtime from specific location

Did you know that you do not have to put an assembly that an application must use at runtime in the bin folder of the application. You can put the assembly in any folder on the system, and then you can refer to the assembly at runtime.
 
In order to do so you can use the AssemblyResolve event. Here is the code example:
 

AppDomain currentDomain = AppDomain.CurrentDomain;

currentDomain.AssemblyResolve += new ResolveEventHandler(CustomResolveEventHandler);

 

private Assembly CustomResolveEventHandler(object sender, ResolveEventArgs args)

{

    //This handler is called only when the common language runtime tries to bind to the assembly and fails.

    //Retrieve the list of referenced assemblies in an array of AssemblyName.

    Assembly MyAssembly, objExecutingAssemblies;

    string strTempAssmbPath = "";

 

    objExecutingAssemblies = Assembly.GetExecutingAssembly();

    AssemblyName[] arrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies();

 

    //Loop through the array of referenced assembly names.

    foreach (AssemblyName strAssmbName in arrReferencedAssmbNames)

    {

         //Check for the assembly names that have raised the "AssemblyResolve" event.

         if (strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(",")))

         {

             //Build the path of the assembly from where it has to be loaded.               

             strTempAssmbPath = "C:\\Myassemblies\\" + args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";

             break;

          }

 

     }

     //Load the assembly from the specified path.                   

     MyAssembly = Assembly.LoadFrom(strTempAssmbPath);

     //Return the loaded assembly.

     return MyAssembly;

 }

Comments

Eran Kampf said:

What I dont get is this:

You loop through all the eferenced assembly names and when you find the AssemblyName in the list you build the assembly path (strTempAssmbPath) using args.Name.

You could have done that streight away without looping through all the referenced assembly names (you're not using strAssmbName for anything...)

Regards,

Eran

# July 3, 2008 11:11 AM

Eran Kampf said:

You're also possibly calling Assembly.LoadFrom with strTempAssmbPath="".

Why not calling it and returning the assembly instead of calling "break;" ?

# July 3, 2008 11:25 AM

Rotem Bloom said:

Hi Eran,

on this example the strAssmbName is used for checking which assembly names have raised the "AssemblyResolve" event. I'm sure there are more options for implementing this event. On this example I show one of the options.

Try to write short application and then debug it. If you need my help with the code for the application let me know.

Bye Rotem

# July 3, 2008 1:05 PM

Eran Kampf said:

Hi Rotem,

 Look inside the if block where you check the assembly name...

Once you "find" the assembly name in  the list of referenced assembly you're not using it to build the assembly path - strAssmbName is not used inside that if block.

You could replace the entire code written with 3 lines:

1. strTempAssmbPath = "C:\\Myassemblies\\" + args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";

2. MyAssembly = Assembly.LoadFrom(strTempAssmbPath);

3. return MyAssembly

# July 3, 2008 4:08 PM

Ariel Ben Horesh said:

mmm... I believe Eran is right.

# July 3, 2008 4:20 PM

Rotem Bloom said:

First of all Eran thanks for your comments,

I totally agree with them. You can make the code shorter.

Thanks :-)

Rotem

# July 3, 2008 5:33 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: