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

12

42

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.

Marmoreal answered 20/6, 2016 at 9:13 Comment(1)
Encountered a similar issue, a colleague had a session open on the server where the CertifyTheWeb application still had a lock on the "acme-challenge" file. Signed him off from the server and the deploy succeeded again. Just leaving this here for anyone who would happen to stumble on this.Higgs
B
53

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

Bistre answered 15/12, 2016 at 17:13 Comment(3)
One observation is that it solves the deploy issue, but because the item is renamed it will not be picked up the next time the app reloads. So the exe that was renamed will not really start the next time, but rather the old exe will still be used.Cerell
@GeorgeOnofrei yeah I think your mileage will vary on this. I've seen others describing the issue you're talking about but for me personally it seems to be picking up the new DLL fine.Bistre
Saw this and went to add it. Was already there. Doesn't solve for meIrvinirvine
J
19

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.

Jabalpur answered 5/7, 2017 at 13:47 Comment(2)
i think from all the solutions this one is the bestPollinate
agree, this is the one we are using to deploy a .NET Core web apiKnisley
C
14

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

enter image description here

Cuss answered 14/4, 2018 at 16:13 Comment(2)
This is ok for an emergency but not really practical for everyday deployment. Plus if you have active clients you should click stop because otherwise they're just going to trigger the problem while you're still deploying.Decoy
Also functions now support slots and probably slot swapping so you can deploy to a logical 'node A' / 'node B' and swap themDecoy
D
13

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

Dolichocephalic answered 18/12, 2018 at 13:9 Comment(0)
G
8

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

Grouch answered 21/6, 2016 at 8:12 Comment(4)
Your answer was useful as usual, thanks Eddie. The only thing that was missing was a way to specify the deployment slot but it was easy enough to change your scripts to do it.Damek
Thank you, this is very helpful! I have one question: How and where do you upload Powershell-Scripts? Do you incorporate them into your Solution. I have a asp.net-app.Winebibber
@Winebibber Just check it into your source control. And there is an extension you can use to do this more easily now: marketplace.visualstudio.com/…Grouch
Thank you! There are two very similar extensions. The one that worked for me was: marketplace.visualstudio.com/… (it also supports slots)Winebibber
N
7

Taking website offline while release should do the trick.

enter image description here

Nicobarese answered 5/5, 2017 at 9:23 Comment(5)
This works. I would only do this, or any script based app restart on a staging slot, for obvious reasons..Deaver
How come I don't have rename locked files?Decoy
@Decoy the screenshot was taken 15 months ago. I am sure they have also released new versions for releasing. So they might have moved this option somewhere else or completely deprecated it. Have a look around or try to change the version.Nicobarese
Thanks. They're definitely still recommending it because It said so in the error message!Decoy
Your solution was the only worked for me as my problem was in IaaS (vm) and not PaaS (web-app). However, I add to also check the following options: "Remove Additional Files at Destination" and "Exclude Files from the App_Data Folder"Dibromide
O
5

Juste add

<EnableMSDeployAppOffline>true</EnableMSDeployAppOffline>

to your publish profile(.pubxml).

Office answered 7/6, 2020 at 18:4 Comment(0)
J
2
  1. Stop app service
  2. deploy code
  3. Start app service

enter image description here

Junta answered 24/5, 2018 at 21:18 Comment(0)
C
2

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.

Cosec answered 11/8, 2020 at 3:46 Comment(0)
D
0

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!!

Drug answered 20/6, 2016 at 13:7 Comment(0)
D
0

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}"
Damek answered 26/8, 2016 at 14:45 Comment(0)
G
0

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

Take app offline

Geary answered 10/3, 2021 at 12:59 Comment(1)
I did the same configuration for my on-prem but it didn't help me.Vivyanne

© 2022 - 2024 — McMap. All rights reserved.