The site DLL seems to be intermittently locked when publishing
Asked Answered
L

2

6

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?

Lament answered 27/9, 2017 at 18:8 Comment(0)
O
9

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/

Ovovitellin answered 29/9, 2017 at 21:6 Comment(1)
4. Deploy it to a "staging" deployment slot on Azure and on the slot have the "Auto Swap Slots" option turned on to swap it with the live site once it is loaded.Byrne
A
0

In my case (Blazor Net 8), publishing to folder (not Azure) this does't work.

Solution: Add an App_Offline.htm with powershell before publish:

  1. Add to your PublishProfile (eg. /Properties/Publishprofile/FolderProfile.pubxml) value <IsPublish>True</IsPublish>

  2. Go to your project file and add the following:

<Target Name="MessageBeforePublish" AfterTargets="Build" Condition="'$(IsPublish)' == 'True'">
        <Message Text="Before publishing...$(PublishUrl)" Importance="high" />
        <Exec Command="powershell -Command &quot;$null = New-Item -Path '$(PublishUrl)' -Name 'App_Offline.htm' -ItemType 'file' -Force&quot;" />
</Target>

And remove after publish:

<Target Name="MessageAfterPublish" AfterTargets="Publish" Condition="'$(IsPublish)' == 'True'">
    <Message Text="After publishing...$(PublishUrl)" Importance="high" />
    <Exec Command="powershell -Command &quot;Start-Sleep -Seconds 3; Remove-Item -Path '$(PublishUrl)\App_Offline.htm'&quot;" />
</Target>
Audun answered 31/7 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.