.netasp.net-core.net-corevisual-studio-2017webdeploy

The site DLL seems to be intermittently locked when publishing


I try to deploy my .net core site to Azure via Publish context menu in VS2017 and occasionally (about 1 in 3 deploys), I get the error below.

Web deployment task failed. (Web Deploy cannot modify the file 'MyCoreWebSite.dll' on the destination because it is locked by an external process. In order to allow the publish operation to succeed, you may need to either restart your application to release the lock, or use the AppOffline rule handler for .Net applications on your next publish attempt. Learn more at: http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_FILE_IN_USE.)

Then I'll literally wait a minute, try again and it will work. Meanwhile, I'll run the handle utility looking for anything locking the DLL and it never finds anything.

Is this a bug or am I missing something simple?


Solution

  • ASP.NET core does not support shadow copying of files, which means that the ASP.NET Core process (Kestrel) will keep locks on those files. You can work around this:

    1. Provide an app_offline.htm page before publish and remove it afterwards. I think there is automated support for this in the publish profile file ( <EnableMSDeployAppOffline>true</EnableMSDeployAppOffline>). But you can do it with custom deploy scripts if you want. In ASP.NET Core 1.x I had also to add a file watch task in the Startup.cs file to shut down the current process. I don't know if it is still needed for ASP.NET Core 2.0.
    2. The disadvantage with 1. is that your site will be offline during the publish. If you don't want that you can work with a copy of your site: copy all files to a subdirectory (eg. \PREVIOUS), change the tag in web.config as such that it points to the .exe in the new subdirectory. Now all locks in your root directory should be gone... Publish your site and change the web.config again afterwards.
    3. If your website is load balanced you can off course take out one server from the pool, update it and add it back to the pool when done.

    More Info

    https://devblogs.microsoft.com/dotnet/web-publishing-updates-for-app-offline-and-usechecksum/