1

I am trying to load an assembly into an application domain. I can successfully load the assembly into the current domain, but I need to load it into an application domain so that I can unload it. The code is as follows:

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\XXX\\XXX.dll";

AppDomain xxxDomain = AppDomain.CreateDomain("XXX Domain");
Type type = typeof(ProxyDomain);
ProxyDomain xxxInstance = (ProxyDomain)xxxDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);

Assembly xxxAssembly = xxxInstance.GetAssembly(path);

However, when I call Load, I get the following error:

Could not load file or assembly XXX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

The ProxyDomain class is as follows:

class ProxyDomain : MarshalByRefObject
{
    public Assembly GetAssembly(string path)
    {
        try
        {
            return Assembly.LoadFrom(path);
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException("Failed to load assembly from path: " + path, ex);
        }
    }
}

Thank you in advance.

Evil August
  • 411
  • 6
  • 18
  • If you debug the path, does a file exist at that location? Also, if that file has dependencies (it references another assembly) it can cause this problem too. – Matthew Mar 14 '14 at 15:38
  • 2
    Does the mentioned assembly have any dependencies ? You may try to hook to xxxDomain.AssemblyResolve event and debug event handler to see what is app trying to resolve. – Ondrej Svejdar Mar 14 '14 at 15:42
  • Yes, it does have dependencies. How do I resolve them? – Evil August Mar 14 '14 at 15:45
  • The dependencies are in the same directory. – Evil August Mar 14 '14 at 15:50
  • I've updated the code to use a proxy and am still getting the same error. – Evil August Mar 14 '14 at 16:35
  • I don't see how inheriting from MarshalByRef object is going to solve anything? What's the purpose of this? Or are you not showing all of the code... I think that what you need is to implement a listener for the assemblyresolve event, as @Ondrej said.. http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve(v=vs.110).aspx – Tewr Mar 14 '14 at 17:05

0 Answers0