none
Problems loading assembly into new AppDomain - System.IO.FileNotFoundException RRS feed

  • General discussion

  • Hi everyone

    I have an issue loading an assembly into a new AppDomain when the directory holding it is not in one of the 'standard' Fusion-checked directories. Just for information I'm using Visual Studio 2008 and .NET 3.5 SP1.

    I have a C# assembly (a class lib) which resides in a subdirectory beneath the execution directory of a Windows Forms stub. So the directory for the exe, for example, is "c:\code\AppDomainPrototype\Bin\Debug" and the subdirectory which contains my class library is "c:\code\AppDomainPrototype\Bin\Debug\Clients\ServiceClients". The dll is called "AppDomainPrototypeClasses.dll"

    Using the following code I get a System.IO.FileNotFoundException:
    AppDomainSetup tempAppDomainSetup = new AppDomainSetup();
    tempAppDomainSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
    tempAppDomainSetup.ApplicationName = "AppDomainPrototype";
    tempAppDomainSetup.PrivateBinPath = "clients\\serviceclients\\bitlocker28";
    tempAppDomainSetup.CachePath = "clients\\serviceclients\\bitlocker28";
    
    AppDomain myTempAppDomain = AppDomain.CreateDomain(
       Guid.NewGuid().ToString(),
       AppDomain.CurrentDomain.Evidence,
       tempAppDomainSetup);
    
    try
    {
       Assembly testAssembly = myTempAppDomain.Load(new AssemblyName("AppDomainPrototypeClasses"));
    }
    catch (Exception ex)
    {
       System.Diagnostics.Debug.WriteLine(ex.Message);
    }
    I have used the Fusion Log Viewer to try and identify the problem but so far I'm stuck...because this is what I'm seeing...

    Remembering that the logs are shown in reverse order in the Viewer, the second-to-last entry reads like this:

    *** Assembly Binder Log Entry  (10/08/2009 @ 16:24:35) ***
    
    The operation failed.
    Bind result: hr = 0x80070002. The system cannot find the file specified.
    
    Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
    Running under executable  f:\Source Code\SourceJRW\AppDomainPrototype\AppDomainPrototype\bin\Debug\AppDomainPrototype.vshost.exe
    --- A detailed error log follows. 
    
    === Pre-bind state information ===
    LOG: User = IUSER\603358508
    LOG: DisplayName = AppDomainPrototypeClasses, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
     (Fully-specified)
    LOG: Appbase = file:///f:/Source Code/sourcejrw/AppDomainPrototype/AppDomainPrototype/bin/Debug/
    LOG: Initial PrivatePath = NULL
    LOG: Dynamic Base = NULL
    LOG: Cache Base = NULL
    LOG: AppName = NULL
    Calling assembly : (Unknown).
    ===
    LOG: This bind starts in default load context.
    LOG: Using application configuration file: f:\Source Code\sourcejrw\AppDomainPrototype\AppDomainPrototype\bin\Debug\AppDomainPrototype.vshost.exe.Config
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
    LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
    LOG: Attempting download of new URL file:///f:/Source Code/sourcejrw/AppDomainPrototype/AppDomainPrototype/bin/Debug/AppDomainPrototypeClasses.DLL.
    LOG: Attempting download of new URL file:///f:/Source Code/sourcejrw/AppDomainPrototype/AppDomainPrototype/bin/Debug/AppDomainPrototypeClasses/AppDomainPrototypeClasses.DLL.
    LOG: Attempting download of new URL file:///f:/Source Code/sourcejrw/AppDomainPrototype/AppDomainPrototype/bin/Debug/Clients/ServiceClients/AppDomainPrototypeClasses.DLL.
    LOG: Attempting download of new URL file:///f:/Source Code/sourcejrw/AppDomainPrototype/AppDomainPrototype/bin/Debug/Clients/ServiceClients/AppDomainPrototypeClasses/AppDomainPrototypeClasses.DLL.
    LOG: Attempting download of new URL file:///f:/Source Code/sourcejrw/AppDomainPrototype/AppDomainPrototype/bin/Debug/AppDomainPrototypeClasses.EXE.
    LOG: Attempting download of new URL file:///f:/Source Code/sourcejrw/AppDomainPrototype/AppDomainPrototype/bin/Debug/AppDomainPrototypeClasses/AppDomainPrototypeClasses.EXE.
    LOG: Attempting download of new URL file:///f:/Source Code/sourcejrw/AppDomainPrototype/AppDomainPrototype/bin/Debug/Clients/ServiceClients/AppDomainPrototypeClasses.EXE.
    LOG: Attempting download of new URL file:///f:/Source Code/sourcejrw/AppDomainPrototype/AppDomainPrototype/bin/Debug/Clients/ServiceClients/AppDomainPrototypeClasses/AppDomainPrototypeClasses.EXE.
    LOG: All probing URLs attempted and failed.
    

    But it looks like the assembly actually is getting loaded at some point because I'm subsequently getting this entry:

    *** Assembly Binder Log Entry  (10/08/2009 @ 16:24:35) ***
    
    The operation was successful.
    Bind result: hr = 0x0. The operation completed successfully.
    
    Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
    Running under executable  f:\Source Code\SourceJRW\AppDomainPrototype\AppDomainPrototype\bin\Debug\AppDomainPrototype.vshost.exe
    --- A detailed error log follows. 
    
    === Pre-bind state information ===
    LOG: User = IUSER\603358508
    LOG: DisplayName = AppDomainPrototypeClasses
     (Partial)
    LOG: Appbase = file:///f:/Source Code/sourcejrw/AppDomainPrototype/AppDomainPrototype/bin/Debug/
    LOG: Initial PrivatePath = clients\serviceclients\bitlocker28\jim\test\3
    LOG: Dynamic Base = f:\Source Code\SourceJRW\AppDomainPrototype\AppDomainPrototype\bin\Debug\clients\serviceclients\bitlocker28\jim\test\3\834d3697
    LOG: Cache Base = f:\Source Code\SourceJRW\AppDomainPrototype\AppDomainPrototype\bin\Debug\clients\serviceclients\bitlocker28\jim\test\3
    LOG: AppName = AppDomainPrototype
    Calling assembly : (Unknown).
    ===
    LOG: This bind starts in default load context.
    LOG: Using application configuration file: f:\Source Code\sourcejrw\AppDomainPrototype\AppDomainPrototype\bin\Debug\AppDomainPrototype.vshost.exe.Config
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
    LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
    LOG: Attempting download of new URL file:///f:/Source Code/sourcejrw/AppDomainPrototype/AppDomainPrototype/bin/Debug/AppDomainPrototypeClasses.DLL.
    LOG: Attempting download of new URL file:///f:/Source Code/sourcejrw/AppDomainPrototype/AppDomainPrototype/bin/Debug/AppDomainPrototypeClasses/AppDomainPrototypeClasses.DLL.
    LOG: Attempting download of new URL file:///f:/Source Code/SourceJRW/AppDomainPrototype/AppDomainPrototype/bin/Debug/clients/serviceclients/bitlocker28/jim/test/3/834d3697/AppDomainPrototype/AppDomainPrototypeClasses.DLL.
    LOG: Attempting download of new URL file:///f:/Source Code/SourceJRW/AppDomainPrototype/AppDomainPrototype/bin/Debug/clients/serviceclients/bitlocker28/jim/test/3/834d3697/AppDomainPrototype/AppDomainPrototypeClasses/AppDomainPrototypeClasses.DLL.
    LOG: Attempting download of new URL file:///f:/Source Code/sourcejrw/AppDomainPrototype/AppDomainPrototype/bin/Debug/clients/serviceclients/bitlocker28/jim/test/3/AppDomainPrototypeClasses.DLL.
    LOG: Assembly download was successful. Attempting setup of file: f:\Source Code\sourcejrw\AppDomainPrototype\AppDomainPrototype\bin\Debug\clients\serviceclients\bitlocker28\jim\test\3\AppDomainPrototypeClasses.dll
    LOG: Entering download cache setup phase.
    LOG: Assembly Name is: AppDomainPrototypeClasses, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
    LOG: A partially-specified assembly bind succeeded from the application directory. Need to re-apply policy.
    LOG: Using application configuration file: f:\Source Code\sourcejrw\AppDomainPrototype\AppDomainPrototype\bin\Debug\AppDomainPrototype.vshost.exe.Config
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
    LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
    WRN: A duplicate assembly was found while copying the assembly item to the cache.
    LOG: Binding succeeds. Returns assembly from f:\Source Code\SourceJRW\AppDomainPrototype\AppDomainPrototype\bin\Debug\clients\serviceclients\bitlocker28\jim\test\3\AppDomainPrototype\assembly\dl3\e5180d90\109ae490_c819ca01\AppDomainPrototypeClasses.dll.
    LOG: Assembly is loaded in default load context.
    Which seems to be telling me that the assembly is indeed loaded.

    As a side note, if I add the following to the app.config file then everything works fine:

    <configuration>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <probing privatePath="Clients\ServiceClients" />
        </assemblyBinding>
      </runtime>
    </configuration>
    The problem here is that the clients will live in a dynamically created subdirectory under the Clients\ServiceClients directory so although I can add the directory to the privatePath, the changed value does not get picked up until the application is restarted. Unloading and reloading the parent application is precisely the situation I'm tasked with avoiding.

    So the conclusion I'm coming to is that perhaps the default AppDomain, not just my newly created AppDomain, needs to know where to probe for the assembly I'm trying to load...but how does that make sense?

    Eternally grateful for any pointers on this one!

    Best regards and thanks...

    Jim
    Architect, Developer, Author, NPC
    Monday, August 10, 2009 3:35 PM

All replies


  • Isn't it funny how writing something down helps solve the problem...

    AppDomain.CurrentDomain.AppendPrivatePath("path to class library");
    

    Adding this line gives Fusion the ability to find the class library...dunno why the current AppDomain needs to know where it is but, hey, it works.

    Thanks to anyone who has viewed this thread already!

    Jim

    Architect, Developer, Author, NPC
    Monday, August 10, 2009 3:48 PM
  • Isn't that a deprecated method?  Basic production code on deprecated methods...sounds scary...
    Wednesday, August 26, 2009 10:02 PM
  • Hi,

        Assembly is loading in new appdomain and it is not loaded in default appdomain,so you are getting error. I am also getting same problem but haven't got yet solution.

    Monday, December 2, 2013 9:29 AM