user2818430
user2818430

Reputation: 6029

ASP.NET Core restart multiple instances

I found quite an interesting way to restart an ASP.NET Core application programmatically.

 public class Program
{
    private static CancellationTokenSource cancelTokenSource = new CancellationTokenSource();

    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseApplicationInsights()
            .Build();

        host.Run(cancelTokenSource.Token);
    }

    public static void Shutdown()
    {
        cancelTokenSource.Cancel();
    }
}

and then in controller:

 public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    public IActionResult Restart()
    {
        Program.Shutdown();
        return RedirectToAction("Index", "Home", null);
    }      
}

It works really good. This will actually shutdown the application but the next request to the app it will start it.

Now how will I achieve the same when I have multiple instances installed like for example in Azure?

Upvotes: 0

Views: 344

Answers (1)

Set
Set

Reputation: 49769

One way is to get the list of all instances from Azure, and do separate HTTP call to each of them using instance endpoint IP address instead of host name. Unfortunately, don't know how to do this exactly in Azure, but in Amazon Cloud we use AmazonEC2Client.DescribeInstances request, and there should be something similar.

Another way is to use the message queue. So each instance should subscribe to the topic and do a restart when receiving the message.

Upvotes: 2

Related Questions