9

I'm attempting to create a script to simplify the process of publishing a .NET Core website. I'm running into an issue when I run dotnet publish against an already running server. The server is IIS with the dotnet bundle installed, so IIS uses its app pool to start dotnet.

Here's my batch file. I'm happy to use another script type:

cd src/app
dotnet build --no-incremental
dotnet publish --framework netcoreapp1.0 --configuration Release --output ../../dist

When I run the script I get this error:

"The process cannot access the file 'C:\inetpub\wwwroot\app\dist\app.dll' because it is being used by another process."

This makes sense, it appears I need to stop, deploy, and restart dotnet. Can I do this from the script? Or is my approach to this problem wrong?

Josiah
  • 3,008
  • 1
  • 34
  • 45
  • Publishing doesn't run anything. How did you run the application? `C:\inetpub\wwwroot\ ` is the default root folder of IIS. If the web app runs under IIS, you'll have to restart its application pool or IIS itself with `iisreset` – Panagiotis Kanavos Mar 16 '17 at 14:11
  • @PanagiotisKanavos - You surmise correctly. See edit. Restarting the App Pool seems to work. Want to post an answer? – Josiah Mar 16 '17 at 14:21

4 Answers4

16

The best way is to drop an app_offline.htm file to your application folder. This will make IIS stop your application and serve the contents of the app_offline.htm file to the user while you are copying the new version. Once you complete copying the new version of your application remove the app_offline.htm file and IIS will start your application. You can find more details on running ASP.NET Core applications with IIS in my post.

Pawel
  • 31,342
  • 4
  • 73
  • 104
1

Based on Pawel's answer, I have a deploy folder containing my app_offline.html file and multiple deploy scripts to IIS. Here's a sample script I use to deploy:

copy .\app_offline.htm C:\hosting\my-project\app_offline.htm
dotnet publish ../MyProject.csproj -r win-x64 -f netcoreapp2.1 --self-contained -c Release -o C:\hosting\my-project
del C:\hosting\my-project\app_offline.htm
Samuel Poirier
  • 1,240
  • 2
  • 15
  • 30
  • To avoid nested publish folder issue described in [here](https://github.com/aspnet/websdk/issues/179), I suggest using a publish output folder inside `bin` folder like `dotnet publish ../MyProject.csproj -r win-x64 -f netcoreapp2.1 --self-contained -c Release -o C:\hosting\my-project\bin\Publish` – Beytan Kurt Jul 19 '19 at 09:26
0

I think this is a valid solution, but doesn't help when I want to script the build process.

Stop-Website "xxx"
Stop-WebAppPool "xxx"
Start-Sleep -Seconds 5

dotnet publish --output d:\publocation

Stop-WebAppPool "xxx"
Start-Website "xxx"
Nate
  • 131
  • 1
  • 2
  • 7
  • Please explain how your solution solves the OP's problem. – Liran Funaro May 15 '17 at 21:57
  • I found that if I, through the IIS management console stopped the website, then I could publish without the file handle error coming up. So I attempted to script the stopping and starting of the website, and thus minimize down time, and potentially schedule the build for off hours. Admittedly I've had mixed results with this, but I am wondering if I include a Stop-WebAppPool, I might have it. I'll include this and report back from my situation. – Nate May 17 '17 at 13:05
  • Updated my solution because I've found that if I script the stopping of the website, and the app pool, the file handle problem is eliminated. In reflecting on the proposed solution of using app_offline.htm, I bet that can be scripted as well. I want to experiment with that too. – Nate May 17 '17 at 13:20
0

if you've created a published profile in Visual Studio and you're using IIS, then you can use that profile instead of writing directly to the destination directory:

dotnet publish /p:PublishProfile=Properties\PublishProfiles\IISProfile.pubxml

symbiont
  • 1,428
  • 3
  • 21
  • 27