Trouble deploying inprocess - existing app won't stop

I am trying to deploy my app as part of a CI/CD script and occasionally get an error because the dlls are still in use when I try and replace them. My script is already copying an app_offline.htm file to the destination folder and waiting 15 seconds. I have also tried deleting the web.config before copying, but still occasionally get a “file being used by another process”.

I was wondering if I could add a service to my app that shuts down the services?

Hi @taglius,

What host are you using? Is this running on IIS, Linux, Windows Service? Depending on this, there are a variety of ways of killing the process running your app as a part of a script that runs on your hosted environment (as opposed to your CI/CD servers).

You could use IApplicationLifetime from within a service to call the StopApplication but I’m not 100% sure what the side effects will be.

Currently IIS, .NET Core 5.0

Hmm. the fact that you are saying it happens occasionally sounds like an app pool issue of some sort. Maybe a fallback to perform an iisreset or some other way of stopping the apppool before copying the files. Eg (though probably a bit of a nuclear option) as shown in this GitHub Actions.

If this is a .NET 5 App you should be able to programatically shut it down by calling IHostApplicationLifetime.StopApplication()

E.g. in a razor page:

@inject IHostApplicationLifetime AppLifetime

<button @onclick="() => AppLifetime.StopApplication()">Restart</button>

Can I get ahold of the IHostApplicationLifetime instance from within a service service? My app doesn’t have any Razor pages.

From your CI/CD Pipeline, in Azure DevOps there is task to restart your app pool or site.

Yes, it’s just a dependency that you can reference as normal:

public class MyServices : Service
{
    public IHostApplicationLifetime AppLifetime { get; set; }
}