Publish has encountered an error ERROR_FILE_IN_USE

I was making a deployment to an ASP.NET Core application from Visual Studio and got this error, see Figure 1.

image

Figure 1, deployment asp.net core web deploy msdeploy from visual studio

Read some of my other web deploy articles here:

Here is the contents of the stack written to the tmp##### file mentioned in Figure 1.

System.AggregateException: One or more errors occurred. —>;
     System.Exception: Build failed. Check the Output window for more details.
     — End of inner exception stack trace —
     at System.Threading.Tasks.Task
                 .ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
     at System.Threading.Tasks.Task
                 .Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
     at Microsoft.VisualStudio.Web
                .Publish.PublishService
                .VsWebProjectPublish.<>c__DisplayClass41_0.<PublishAsync>b__2()
     at System.Threading.Tasks.Task`1.InnerInvoke()
     at System.Threading.Tasks.Task.Execute()
  — End of stack trace from previous location where exception was thrown —
     at System.Runtime.CompilerServices.TaskAwaiter
                .ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter
                .HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.VisualStudio.ApplicationCapabilities.Publish.ViewModel
               .ProfileSelectorViewModel.<RunPublishTaskAsync>d__116.MoveNext()
  —> (Inner Exception #0) System.Exception: Build failed.
                                     Check the Output window for more details.>—
===================

And the Output window contained these details:

Severity    Code    Description    Project    File    Line    Suppression State
  Error        Web deployment task failed. (Web Deploy cannot modify the file <???-core.Views.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.)    ???-core        0

This one hit the nail on the head with its reference to AppOffline, because I was indeed deploying directly to production and there was activity on the site and certainly the ***-core.Views.dll was loaded into memory.

Here is a reference to the AppOffline rule –> “Taking an Application Offline before Publishing”

Here is an article from over a decade ago –> “App_Offline.htm”

Point is and the reason is the code is in use and that is the reason for the ERROR_FILE_IN_USE deployment exception.  Another detail in the output window showed me specifically which file was active.

Severity    Code    Description    Project    File    Line    Suppression State
Warning    TS6053    (JS) File ‘C:/Users/benperk/source/repos/***-core/???-core/Views/
                    Home/???/***/+++/ not found.    ***-core JavaScript Content Files   
                    C:\Users\benperk\source\repos\***-core\???-core\Views\Home\Index.cshtml
                    1    Active

Instead of doing the offline approach, I simply recycled/restarted the App and the deployment worked after that because it shutdown the W3WP process and started a new one.  I only recommend that on a test site.

  • Don’t deploy straight to production
  • Do deploy to a slot or a test site that can be restarted if required
  • Do read about the App_Offline.htm file and how you can implement that into your deployed, if required