Dave New
Dave New

Reputation: 40092

Azure Web App deploy: Web Deploy cannot modify the file on the destination because it is locked by an external process

I am using the "Azure Web App Deployment" build step in VSTS to publish an ASP.NET Core API to an Azure Web App:

Azure Web App Deployment

Occasionally, this step breaks with the following error:

[error]Microsoft.Web.Deployment.DeploymentDetailedClientServerException: Web Deploy cannot modify the file 'MyProject.Api.exe' 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.

This GitHub issue raises the same issue, but there is no suggested solution using the Azure Web App Deployment build step.

Upvotes: 43

Views: 41231

Answers (13)

jbprinsloo
jbprinsloo

Reputation: 1

I had the same issue, which had nothing to do with Visual Studio or Webdeploy. I had some problems with the server when upgrading to the latest version of .net core 8. I then ran the MyAPI.exe file directly on the server for testing purposes but never closed it. This is what has caused the lockup for me on my next deployment.

Silly Mistake, but I hope it can help somebody

Upvotes: 0

VivekDev
VivekDev

Reputation: 25553

Mine is a onprem release pipeline, and this is what I did.

Take app offline

Upvotes: 0

Newclique
Newclique

Reputation: 494

Before you open too many cans of worms on this, wait 30-60 seconds and try publishing your app a second time. It appears that this sometimes happens because the stop request that gets issued to the service either returns before the service has completely stopped or maybe Visual Studio is not waiting long enough for this operation to finish. Each time I've encountered the problem, waiting and retrying the publish worked the second time.

Upvotes: 2

Badr Bellaj
Badr Bellaj

Reputation: 12871

Juste add

<EnableMSDeployAppOffline>true</EnableMSDeployAppOffline>

to your publish profile(.pubxml).

Upvotes: 5

Sibeesh Venu
Sibeesh Venu

Reputation: 21809

I was getting the same error while I was trying to publish my Azure Function App. I followed this Microsoft document and did the following steps.

  1. Right click on your Project and select Edit ....csproj
  2. Add <EnableMSDeployAppOffline>true</EnableMSDeployAppOffline> in PropertyGroup tag

    <PropertyGroup>
      <TargetFramework>netcoreapp2.1</TargetFramework>
      <AzureFunctionsVersion>v2</AzureFunctionsVersion>
      <EnableMSDeployAppOffline>true</EnableMSDeployAppOffline>
    </PropertyGroup>
    
  3. Save and Rebuild your solution

  4. Now publish again

If that didn't work for you, you can always add MSDEPLOY_RENAME_LOCKED_FILES=1 as Mr. Ben mentioned in his answer, to your Application settings. You can do that from Visual Studio itself.

enter image description here

enter image description here

Hope it helps

Upvotes: 13

Kurkula
Kurkula

Reputation: 6762

  1. Stop app service
  2. deploy code
  3. Start app service

enter image description here

Upvotes: 2

kk-dev11
kk-dev11

Reputation: 2674

You can restart the Function App to release the lock. After that you should be able to deploy.

enter image description here

Upvotes: 14

Magnus Johansson
Magnus Johansson

Reputation: 28325

I was struggling with the same locking issue.
There are now a new Tasks (in Preview) that you can add for starting and stopping the App Service:

enter image description here

Add a stop task before deployment and a start task after the deployment.

enter image description here

This did the trick for me.

Upvotes: 19

akd
akd

Reputation: 6740

Taking website offline while release should do the trick.

enter image description here

Upvotes: 7

Ben Barreth
Ben Barreth

Reputation: 1743

As per a separate thread in the Microsoft Github repo here, there's a hacky workaround where if you add the following key to the Azure Appsettings, it can help resolve the locked file deployment error:

MSDEPLOY_RENAME_LOCKED_FILES = 1

I'm not sure how long this appsetting hack will be supported, but it did help solve the issue for me personally.

enter image description here

Upvotes: 54

Rui Jarimba
Rui Jarimba

Reputation: 18149

Eddie's answer was close to what I needed, the only thing that was missing was a way to specify the deployment slot:

stopapp.ps1

param($websiteName, $websiteSlot)
$website = Get-AzureWebsite -Name $websiteName -Slot $websiteSlot
Stop-AzureWebsite -Name $websiteName -Slot $websiteSlot

startapp.ps1

param($websiteName, $websiteSlot)
$website = Get-AzureWebsite -Name $websiteName -Slot $websiteSlot
Start-AzureWebsite -Name $websiteName -Slot $websiteSlot

And then on your Azure PowerShell task Script Arguments could be something like this:

-websiteName "{mywebsite}" -websiteSlot "{mydeploymentslot}"

Upvotes: 0

Eddie Chen - MSFT
Eddie Chen - MSFT

Reputation: 29976

You can create two Power Shell scripts:

stopapp.ps1:

param($websiteName)
$website = Get-AzureWebsite -Name $websiteName
Stop-AzureWebsite -Name $websiteName

startapp.ps1:

param($websiteName)
$website = Get-AzureWebsite -Name $websiteName
Start-AzureWebsite -Name $websiteName

And then add an "Azure PowerShell" task before and after "Azure Web App Deployment" task to stop the web app before deploy and start the app after deploy. enter image description here

Upvotes: 8

baywet
baywet

Reputation: 5382

There are some dedicated tasks for asp.net core projects because the deployment process is a little bit different.
You can get those from the marketplace for free, check out DNX Tasks vsts marketplace
Hope that helps!!

Upvotes: 0

Related Questions