Avoid file locking error with Shadow Copy for ASP.NET Core Apps when publishing

Programming, error messages and sample code > ASP.NET
Shadow copying app assemblies to the ASP.NET Core Module (ANCM) for IIS can provide a better end user experience than stopping the app by deploying an app offline file.
 
Currently .NET locks application binaries when running on Windows making it impossible to replace binaries when the application is still running. In such scenarios, you can enable shadow copying by customizing the ASP.NET Core module handler settings. In most cases, ASP.NET Core application do not have a web.config checked into source control that you can modify (they are ordinarily generated by the SDK). In your production site, add the highlighted lines below to the root web.config.
 

Important

 
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified"/>
        </handlers>
        <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
            <handlerSettings>

                    <!-- This line enables Shadow Copy -->
                    <handlerSetting name="enableShadowCopy" value="true" />
                    
                    <!-- This line sets the destination directory for Shadow Copy. The value "../ShadowCopyDirectory/" specifies the parent directory of the location where web.config is located on the production server. You can update it as you need. -->
                    <handlerSetting name="shadowCopyDirectory" value="../ShadowCopyDirectory/" />

                    <!-- Only enable handler logging if you encounter issues-->
                    <!--
                        <handlerSetting name="debugFile" value="aspnetcore-debug.log" />
                        <handlerSetting name="debugLevel" value="FILE,TRACE" />
                    -->
            </handlerSettings>
        </aspNetCore>
    </system.webServer>
</configuration>
If you usually publish your application through FTP, please only upload the updated files (excluding updates to the web.config).
 
If you usually publish your application through web deploy, to prevent the web.config from being overwritten after deployment, please follow these steps:
 
  1. open your project in your local Visual Studio
  2. right click on the project name, select "Add", choose "New Item...", create "Web.Release.config" file
  3. replace the content in Web.Release.config with the following:
<?xml version="1.0" encoding="utf-8"?>

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <location path="." inheritInChildApplications="false">
        <system.webServer>
            <aspNetCore processPath="%LAUNCHER_PATH%">
                <handlerSettings xdt:Transform="Insert">
                    
                    <!-- This line enables Shadow Copy -->
                    <handlerSetting name="enableShadowCopy" value="true" />
                    
                    <!-- This line sets the destination directory for Shadow Copy. The value "../ShadowCopyDirectory/" specifies the parent directory of the location where web.config is located on the production server. You can update it as you need. -->
                    <handlerSetting name="shadowCopyDirectory" value="../ShadowCopyDirectory/" />
                </handlerSettings>
            </aspNetCore>
        </system.webServer>
    </location>
</configuration>
When you publish your application through web deploy, the generated web.config will be automatically updated.
 
Reference: