Attention: We are retiring the ASP.NET Community Blogs. Learn more >

AppDomains and Shadow Copy Issues

Although this may be a little off-topic post but it may be useful for someone that have the same issue that I found.

If you create an AppDomain inside an aspx page in Windows 2003 (default service account) and you set the Shadow copy to true and the ApplicationBase path is set to some other folder outside your website, an awful exception will be thrown when you try to load an assembly inside this new AppDomain:

System.IO.FileLoadException: Unable to load file 'TestAppDomainLib (Where 'TestAppDomainLib' is the assembly to be loaded in the new AppDomain)

Here is a sample code that will repro this issue and the workaround I found.
 1) Create an assembly with some class (i.e. TestAppDomainLib.MyClass, TestAppDomainLib) and save it to "C:\TestAppDomainLib\bin" 2) In an aspx page (i.e inside a button event) , add the followin code snippet: AppDomainSetup setupInfo = new AppDomainSetup();AppDomainSetup currentSetup = AppDomain.CurrentDomain.SetupInformation;setupInfo.ApplicationName = "MyAppDomain"; // Set the appbase to some directorysetupInfo.ApplicationBase = @"C:\TestAppDomainLib"; // Set the config file to the current virtual directory web.config filesetupInfo.ConfigurationFile = currentSetup.ConfigurationFile; // Add the current "bin" folder path to the appBase folder.// Notice taht you must copy the assembly to be loaded to the "C:\TestAppDomainLib\bin" foldersetupInfo.PrivateBinPath = currentSetup.PrivateBinPath;setupInfo.PrivateBinPathProbe = currentSetup.PrivateBinPathProbe; // Enable shadow copysetupInfo.ShadowCopyFiles = bool.TrueString; // NOTE: If we add this line (set the cache path with i.e, the ASP.NET cache path) this will fix this issue.//setupInfo.CachePath = currentSetup.CachePath; // Create the new AppDomainAppDomain domain = AppDomain.CreateDomain( setupInfo.ApplicationName, null, setupInfo); // We try to load an assembly // (THIS LINE WILL THROW THE EXCEPTION)TestAppDomainLib.MyClass adapter = domain.CreateInstanceAndUnwrap ( "TestAppDomainLib", "TestAppDomainLib.MyClass" ) as TestAppDomainLib.MyClass; 3) Run the above page and the exception shluod be thrown in the line marked with // (THIS LINE WILL THROW THE EXCEPTION) 

If you want further info, I just reported this issue to the Product Feedback Center so hopefully this will be fixed for v2.0

 

No Comments