Deploying a new build of an ASP.NET Core 10 app on IIS fails with locked files and brief 503s; how do app_offline.htm, shadow copying and ANCM's shutdownDelay each help?
answer
- running binaries are locked on Windows
- a file that stops the app
- 503 with the file's HTML as body
- enableShadowCopy in handlerSettings
- 1-second shutdownDelay since .NET 9
basics
~20 sapp_offline.htm makes ANCM stop the app and answer 503 with that page until it is deleted, freeing the files. Shadow copying runs the app from a copy. shutdownDelay, 1 s by default since .NET 9, narrows the recycle race behind 503s.
solid answer
~50 sOn Windows a running app locks its DLLs, so copying a new build over it fails. **`app_offline.htm`**: when ANCM sees that file in the content root it gracefully shuts the app down (waiting up to `shutdownTimeLimit`, 10 s by default), answers every request with **503** and the file's HTML, and the files become replaceable; deleting it makes the next request start the new build. Web Deploy uses the same mechanism. **Shadow copying** (`enableShadowCopy` and `shadowCopyDirectory` handler settings) makes ANCM start the app from a copied folder, so the originals stay writable; IIS still recycles and some requests may still see 503. **`shutdownDelay`** is an ANCM handler setting, or the `ANCM_shutdownDelay` variable, in milliseconds: since the .NET 9 module it defaults to **1 second** between IIS's stop notice and the managed shutdown, reducing the race in which the old app rejects requests before IIS routes them to the new one.
code
powershell · 10 lines$site = 'D:\sites\permits'
# 1. Stop the app and show a maintenance page (ANCM answers 503 with this HTML)
Copy-Item .\maintenance.htm "$site\app_offline.htm"
# 2. Replace the build, never deleting the offline file mid-copy
robocopy .\publish $site /MIR /XF app_offline.htm
# 3. Bring the app back; the next request starts the new build
Remove-Item "$site\app_offline.htm"go deeper
Recall that a running app locks its files on Windows and that dropping app_offline.htm in the content root stops it and shows that page.
Explain what ANCM does while the file exists (503 with the file's HTML), shutdownTimeLimit, and how shadow copying is enabled through handlerSettings.
Script deployments that never delete the offline file mid-copy, tune shutdownDelay from observed 503s during recycles, and know shadow copying still recycles.
Decide when IIS-level techniques are enough for a public service and when to invest in two instances behind a load balancer for true zero downtime.
## The underlying problem When an ASP.NET Core app runs under IIS, Windows holds its assemblies open. Copying `PermitPortal.dll` over the running version fails with a sharing violation, and changing `web.config` restarts the app mid-copy. A deployment therefore has to **stop the app, replace files, start the app**, and the user-visible question is what clients receive while that happens. The ASP.NET Core Module (ANCM) offers three tools. ## Tool 1: `app_offline.htm` ANCM watches the application's content root for a file named exactly `app_offline.htm`. 1. **The file appears.** ANCM begins a graceful shutdown of the app. If the app is still running after `shutdownTimeLimit` (an `aspNetCore` attribute, 10 seconds by default), ANCM stops the process. 2. **While it exists,** ANCM itself answers every request with **503 Service Unavailable**, `Content-Type: text/html`, and the file's contents as the body. ANCM marks the response so IIS custom error pages do not replace it, so users see your maintenance page. 3. **Files are unlocked.** The deployment copies the new build. 4. **The file is removed.** The next request makes ANCM start the app from the new files. Web Deploy places and removes the file automatically; a script can do the same: ```powershell $site = 'D:\sites\permits' Copy-Item .\maintenance.htm "$site\app_offline.htm" robocopy .\publish $site /MIR /XF app_offline.htm Remove-Item "$site\app_offline.htm" ``` The `/XF app_offline.htm` matters: a mirror copy would otherwise delete the file mid-deploy and restart the half-copied app. With out-of-process hosting, an open WebSocket can delay the shutdown. ## Tool 2: shadow copying Shadow copying lets the files be replaced **while the app keeps running**: ```xml <aspNetCore processPath=".\PermitPortal.exe" hostingModel="inprocess"> <handlerSettings> <handlerSetting name="enableShadowCopy" value="true" /> <handlerSetting name="shadowCopyDirectory" value="../ShadowCopyDirectory/" /> </handlerSettings> </aspNetCore> ``` ANCM copies the app into a numbered subfolder of `shadowCopyDirectory` and starts it from there, so the originals stay writable. Logs and `app_offline.htm` are still relative to the real app folder. Copying the new build changes files IIS watches, the app is recycled, and the next start uses a fresh copy. The documentation is explicit that this **reduces** downtime but is not zero-downtime: requests can still receive 503 during the recycle. The app pool identity needs write access to the shadow copy directory. ## Tool 3: `shutdownDelay` During an app pool recycle or a configuration-triggered restart, IIS starts routing new requests to the new instance while the old one stops. If ANCM tells the old app to shut down **immediately**, there is a window in which IIS still hands requests to the old instance, and ANCM rejects them with 503. The ANCM shipped with .NET 9 added a delay between IIS's stop notification and the managed shutdown: - **Default:** 1 second. - **Configure:** `<handlerSetting name="shutdownDelay" value="5000" />` (milliseconds) in `web.config`, or the `ANCM_shutdownDelay` environment variable when no handler setting is present. - **Validation:** a negative value is a configuration error. - **Scope:** ANCM is a machine-wide component from the Hosting Bundle, so the behaviour depends on the installed bundle, not on the app's target framework. The delay does not slow requests; it only postpones the start of the old instance's shutdown. Slower or heavily loaded servers may need more than a second. ## A runbook for the permit portal 1. Confirm the target server's Hosting Bundle version, because `shutdownDelay` behaviour comes from its ANCM. 2. Copy a branded maintenance page in as `app_offline.htm` and wait for ANCM to stop the app (at most `shutdownTimeLimit`). 3. Mirror the new publish output, excluding `app_offline.htm` and any server-specific files. 4. Remove `app_offline.htm`, then request a warm-up URL so the first citizen does not pay the startup cost. 5. Watch the IIS logs for 503s during the next scheduled recycle and raise `shutdownDelay` if they cluster at the switch-over. ## Choosing among them | Need | Use | |---|---| | simple, predictable maintenance window | `app_offline.htm` with a friendly page | | fewer failed requests during routine deploys | shadow copying, accepting a recycle | | fewer 503s during recycles | raise `shutdownDelay` | | genuinely zero downtime | two instances behind a load balancer, switched over | ## Related settings that are not in this picture - `HostOptions.ShutdownTimeout` and `IHostApplicationLifetime` decide how the app itself drains once ANCM starts the shutdown. - App pool recycling schedules and overlapped recycle are IIS settings, not ANCM ones.
- What status code and body does a client get while app_offline.htm is present?ANCM answers 503 Service Unavailable with `Content-Type: text/html` and the file's contents as the body, and it asks IIS not to substitute a custom error page. The app itself is not running, so no middleware, logging or health endpoint of yours responds.
- Why can shadow copying still produce 503 responses?Shadow copying only removes the file lock. Replacing the files still causes IIS to recycle the application, and during the switch the old instance stops while the new one starts, so some requests can be rejected. It reduces downtime but does not remove it.
- Where does ANCM read shutdownDelay from if web.config has no handler setting?From the `ANCM_shutdownDelay` environment variable, in milliseconds. If neither is set, it uses the 1-second default; a negative value is rejected as a configuration error.
- app_offline.htm is served by the app's static file middleware.
- Shadow copying gives true zero-downtime deployments.
- shutdownDelay delays every incoming request by that many milliseconds.
- shutdownDelay is configured in appsettings.json.
- Deleting the DLLs first is a safe way to release the locks.
Assumes the .NET 10 Hosting Bundle; the 1-second default shutdownDelay arrived with the ASP.NET Core Module shipped for .NET 9.