If you deploy to Azure App Service Web Apps often enough, eventually you will run into the dreaded error below:
Web Deploy cannot modify the file 'Microsoft.CodeAnalysis.CSharp.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.
The only solution is to restart the Web App and attempt your deployment again. When you are implementing DevOps, you cannot have an inconsistent pipeline. We need a solution that will guarantee a consistent deployment. I am sure many of you are thinking, “We use Deployment Slots so we are safe.” Well, not completely. If you run smoke tests on your staging slot before swapping into production, future deployments to your staging slot could result in the same error. The only way that I have achieved 100% reliability is to stop the slot you plan to deploy to then deploy into the stopped slot. After you safely deploy your changes, you restart the slot and finally swap it into production.
Now to implement this in a DevOps pipeline you need a way to automate the stopping, starting, and swapping of my slots. Visual Studio Teams Services recently added a Start/Stop Task. This task allows you to stop a single slot of a Web App while leaving the other slots running. Now you can safely stop the slot you want to deploy to, use the Azure Web App Deployment Task, restart the slot, and finally swap the slots.