Search code examples
asp.net-coreservice-workerstartup

How do you deploy a long running .NET Core application as an App Service?


I have an application that mimics an equity market. One part of it, generates price changes and POSTS them to a consumer sending roughly 100 price changes roughly ten times a second. The second part of the market exchange takes in orders, executes them randomly and asynchronously sends execution reports back to the same consumer of the price changes.

I want to put this on a App Service, but here's the issue:

  1. I want the price generator to start immediately and run continuously.
  2. The order execution only needs to run when orders are sent (asynchronously) and all the orders have been executed or cancelled. Then it can shut down until another order is received.

It seems like I'm forced into one of two buckets and neither applies to what I want to do. A Web Job appears to work like a Service in Windows 11. It will start up immediately and run until you shut it down, but it doesn't have the logic to handle an ASP-type controller.

Deploying as an App Service works as long as I wake it up by POSTing an order, but the price feed doesn't start until I send the order.

So here's the question: How do you deploy a .NET Core application as an App Service and have it start automatically (without waking it up with an initial HTTP call)?


Solution

  • According to your description, I suggest you could consider modifying the price feed as a background service inside the .net core application. The background tasks can be implemented as hosted services. A hosted service is a class with background task logic that implements the IHostedService interface.

    It contains the StartAsync method. The StartAsync(CancellationToken) contains the logic to start the background task. StartAsync is called before:

    The app's request processing pipeline is configured. The server is started and IApplicationLifetime.ApplicationStarted is triggered.

    More details, you could refer to this article.

    Besides, I suggest you could also set the Azure web app's configuration as alwayson.