Recently I was using a new AppDomain from within a Visual Studio package to reflect over a dll and get some metadata. I then unloaded the AppDomain expecting that the file locks on the dll file would be released…..but they weren’t.
I could see that another AppDomain in VS was holding a lock on my file. You can see this by switching on Native debugging when attaching another visual studio instance and from the immediate window use:
.load sos
!dumpdomain
It turns out this is due to the LoaderOptimization attribute. Set this property to SingleDomain on your AppDomainSetup object to allow the file lock to be freed when unloading your AppDomain. This translates as.
Indicates that the application will probably have a single domain, and loader must not share internal resources across application domains.





Hi, I am also facing the same issue. Can you tell me what you did to resolve the problem. Can you send me the code snippet to do the trick. That would be grateful.
Don’t have the code to hand but if you follow the link you should see the code that sets the LoaderOptimization property on the AppDomainSetup object.
AppDomainSetup mySetupInfo = new AppDomainSetup();mySetupInfo.ApplicationBase = domainDir;
mySetupInfo.ApplicationName = executableNameNoExe;
mySetupInfo.LoaderOptimization = LoaderOptimization.SingleDomain;
Thanks, How did you load your assembly in new app domain I tried different options but finally hit the wall. Please help.