I have an ASP.NET site that does CreateInstanceAndUnwrap:
var setup = new AppDomainSetup
{
ShadowCopyFiles = "true",
ShadowCopyDirectories = "true",
ApplicationBase = textPipe["host"] == "web" ? Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath) : Environment.CurrentDirectory,
ConfigurationFile = $"UnitTests/{testProject}/app.config"
};
var domain = AppDomain.CreateDomain("UnitTestDomain", null, setup);
var type = typeof(TestProxy);
var instance = (TestProxy)domain.CreateInstanceAndUnwrap(
type.Assembly.FullName,
type.FullName);
All works, but after I run this code under IISExpress on my dev workstation, I can no longer recompile the DLLs -- the error I get is "Unable to copy file .... because it is being used by another process." I have to stop IISExpress before recompiling. This will be definitely be a bigger issue in production, if it will be necessary to stop AppPool prior to each deployment.
Not sure why the files are locked? I am using the ShadowCopyFiles and ShadowCopyDirectories, but that doesn't seem to help.
What can I do to avoid locking files? Thank you.
It's been a while since I've had to deal with AppDomain, but I think maybe calling
AppDomain.Unload(domain)
at the end of the relevant request should free the assembly. If there is a need for it to be in memory continually (if it is too expensive to reload per request), then unloading the domain probably won't work for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With