Why can't ASP.NET Core DLL files be replaced during deployment?
A common problem when manually deploying an ASP.NET Core application to IIS is being unable to overwrite some of the existing DLL files.
You may encounter messages such as:
Access to the path is denied. The process cannot access the file because it is being used by another process. Cannot replace MyProject.dll. File is locked by another process.
At first this may appear to be a Windows permissions problem. In many ASP.NET Core deployments, however, the actual reason is much simpler.
The real problem:
The ASP.NET Core application is still running and using deployment DLL files, so Windows may prevent those files from being overwritten.
How does this happen?
Imagine that a new version of the application has been published from Visual Studio.
The output contains:
publish │ ├── MyProject.dll ├── MyProject.exe ├── MyProject.deps.json ├── MyProject.runtimeconfig.json ├── web.config ├── wwwroot └── additional DLL files
Static files may copy successfully while DLL files used by the running application fail to update.
How ASP.NET Core runs behind IIS
When ASP.NET Core is hosted on IIS, the ASP.NET Core Module (ANCM) manages the connection between IIS and the application.
User ↓ IIS ↓ ASP.NET Core Module ↓ ASP.NET Core Application ↓ Application DLLs
While the application is running, files in the deployment directory may remain locked.
One solution: Stop the IIS Application Pool
The application pool can be manually stopped from IIS Manager before copying the new deployment.
IIS Manager ↓ Application Pools ↓ MyProjectPool ↓ Stop
This releases the application files and allows the deployment to continue.
However, ASP.NET Core provides a convenient mechanism specifically designed for this scenario: app_offline.htm.
Solution: app_offline.htm
Place a file named:
app_offline.htm
in the root directory of the deployed ASP.NET Core application.
The ASP.NET Core Module detects the file and attempts to gracefully shut down the application.
The workflow becomes:
Create app_offline.htm ↓ ASP.NET Core Module detects it ↓ Application shuts down ↓ Deployment DLLs are released ↓ Copy the new publish files ↓ Delete app_offline.htm ↓ The next request starts the application
Where should app_offline.htm be placed?
It should be placed in the deployed application's root directory, normally the same directory that contains web.config.
C:\inetpub\wwwroot\MyProject\ │ ├── app_offline.htm ├── web.config ├── MyProject.dll ├── MyProject.exe ├── MyProject.deps.json └── wwwroot
Example app_offline.htm
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Maintenance</title> </head> <body> <h1>We are performing a short maintenance operation.</h1> <p> The service will be available again shortly. </p> </body> </html>
While the file exists, the ASP.NET Core Module returns its contents to incoming requests.
Recommended deployment order
- Publish the new version.
- Create app_offline.htm in the server application directory.
- Allow the application to shut down.
- Copy the new publish files.
- Verify that the deployment completed successfully.
- Delete app_offline.htm.
- Request the site and verify the new application version.
Publish ↓ Create app_offline.htm ↓ Wait for shutdown ↓ Copy new files ↓ Delete app_offline.htm ↓ Verify website
Why does app_offline.htm release DLL locks?
The file itself does not directly unlock the DLLs.
Instead, it tells the ASP.NET Core Module that the application should be shut down. When the application process exits, the operating system can release the files it was using.
ASP.NET Core Process ↓ Stops ↓ DLL usage ends ↓ Operating system releases files ↓ DLLs can be replaced
What if the files are still locked?
Application shutdown may not always be instantaneous.
Long-running requests, WebSocket connections, SignalR connections or background work can delay shutdown in some hosting scenarios.
For this reason, it is a good idea to allow a short period between creating app_offline.htm and replacing deployment files.
Do not forget to delete app_offline.htm
One of the easiest mistakes to make is completing the deployment but leaving app_offline.htm in the application directory.
As long as the file remains there, users continue to receive the offline page.
Deployment completed ↓ app_offline.htm forgotten ↓ Website still displays maintenance page
Automating the process with PowerShell
$sitePath = "C:\inetpub\wwwroot\MyProject" $publishPath = "C:\Publish\MyProject" $offlineFile = Join-Path $sitePath "app_offline.htm" Set-Content ` -Path $offlineFile ` -Value "<html><body>Maintenance in progress.</body></html>" Start-Sleep -Seconds 3 Copy-Item ` -Path "$publishPath\*" ` -Destination $sitePath ` -Recurse ` -Force Remove-Item $offlineFile -Force
Production scripts should additionally contain error handling, backup and rollback logic.
Take a backup before deployment
For production environments, keeping a copy of the currently working deployment before replacing files makes rollback significantly easier.
Create app_offline.htm ↓ Backup current deployment ↓ Copy new version ↓ Verify files ↓ Delete app_offline.htm ↓ Test application
What about database migrations?
If the new application version also changes the database schema, the deployment order becomes even more important.
Application binaries and database migrations should be planned so that an incompatible application version is not started against the wrong database schema.
Web Deploy also uses this mechanism
The app_offline.htm approach is not merely a workaround. It is an official ASP.NET Core IIS deployment mechanism and is also used by Web Deploy to stop and restart applications during deployment.
When is app_offline.htm useful?
- Hosting ASP.NET Core on IIS
- Manually copying publish files to a server
- Receiving locked DLL errors
- Showing a temporary maintenance page
- Preventing normal application requests during deployment
Conclusion
When DLL files cannot be replaced during an ASP.NET Core deployment to IIS, the problem is often not file permissions. The running application is still using the deployment files.
The app_offline.htm mechanism provides a simple way to shut down the application, release the files and safely copy the new deployment.
The basic workflow is:
1. Create app_offline.htm 2. Wait for the application to stop 3. Copy the new publish files 4. Delete app_offline.htm 5. Verify the site
For developers who manually deploy ASP.NET Core applications to IIS, this simple file can become one of the most useful tools in the deployment process.
0 Comments