PRB: Assembly.LoadFrom Does Not Load Dependent Assemblies That Are Located in the Same Directory

Article translations Article translations
Article ID: 327435 - View products that this article applies to.
This article was previously published under Q327435
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

On This Page

Symptoms

You can call the Assembly.LoadFrom() method on an assembly that invokes a method from a dependent assembly. If the method is located in the same directory, you may see one of the following exceptions:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.


System.IO.FileNotFoundException: File or assembly name assembly, or one of its dependencies, was not found


This occurs when the assembly that you load is in a directory of the same name as the assembly, and the calling application is in a different directory.

Cause

When you dynamically load an assembly with Assembly.LoadFrom() from a directory with the same name as the assembly (for example, MyAssembly.dll is located in the directory that is named MyAssembly), the assembly is loaded in the Load context instead of the LoadFrom context. The dependent assemblies are not loaded.

Resolution

Rename the directory where the assembly is loaded from and where its dependents are located. A directory with a different name causes the assembly to load in the LoadFrom context. The dependents of the assembly are loaded also.

For example, instead of locating MyAssembly.dll and its dependent assembly (DepAssembly.dll) in the directory that is named MyAssembly, put them in a directory that is named MyAssemblyDir.

Status

This behavior is by design.

More information

Steps to Reproduce the Behavior

  1. Create a class library that defines a class. Name it DepAssembly:
    //DepAssembly.cs
    using System;
    using System.Reflection;
    
    public class DepAssembly
    {
    }
    					
  2. Create a class library that is named MyAssembly, and add a reference to DepAssembly.dll.
  3. Add a method that creates an instance of DepAssembly:
    //MyAssembly.cs
    using System;
    using System.Reflection;
    
    public class MyAssembly
    {
    	public void GetDepAssembly()
            { 
               new DepAssembly(); 
            }
    }
    					
  4. Create a Console application that is named MyApp that loads MyAssembly.dll with Assembly.LoadFrom(). Use reflection to create a MyAssembly type and then invoke its method:
    //MyApp.cs
    using System;
    using System.Reflection;
    using System.Runtime.Remoting;
    
    class MyApp
    {
    	static void Main()
    	{
    		// Load the assembly and create an Instance of MyAssembly.
    		Assembly aa = Assembly.LoadFrom("MyAssembly\\MyAssembly.dll");
    		Type a_t = aa.GetType("MyAssembly");
    		object o = Activator.CreateInstance(a_t);
    
    		// Invoke the GetDepAssembly method.
    		object ac = o.GetType().InvokeMember("GetDepAssembly", BindingFlags.InvokeMethod, null, o, new object[] {});
    	}
    }
    					
  5. Arrange the compiled files in the following directory structure:
    \MyApp
    	MyApp.exe
    
    \MyApp\MyAssembly
    	MyAssembly.dll
    	DepAssembly.dll
    						
  6. Run MyApp.exe.

    With this directory structure, MyApp.exe fails with the exception that is described in the "Symptoms" section.

References

For more information about Assembly.LoadFrom(), visit the following Microsoft Web site:

Assembly.LoadFrom Method
For more information about the .NET runtime and how it locates assemblies, visit the following Microsoft Web site:

How the Runtime Locates Assemblies

Properties

Article ID: 327435 - Last Review: October 26, 2013 - Revision: 2.0
Applies to
  • Microsoft Visual Studio .NET 2002 Professional Edition
Keywords: 
kbnosurvey kbarchive kbprb KB327435

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com