Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CreateInstanceAndUnwrap locks up dll under IIEXpress

Tags:

.net

asp.net

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.

like image 923
user1044169 Avatar asked Aug 30 '17 17:08

user1044169


1 Answers

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.

like image 170
Kevin Hirst Avatar answered Oct 18 '22 10:10

Kevin Hirst