Hangfire recurring job stops after app pool gets recycle - asp.net-core

I have ASP.NET Core API (v 2.2) running under IIS using Windows Hosting module that is shipped with .NET Core. I have configured Hangfire in startup.cs with couple of recurring jobs.
The API's app pool periodically gets recycled because of inactivity. However whenever app pool get recycled(for any reason) the recurring job stops running until the first user invoke the api.
Hangfire documentation has solution but it is specific to Full .NET and it may not work for .NET Core.
One of the solution i found is to run recurring jobs in console application but then i have to create and maintain one more application. Also, in addition to recurring jobs, API internally creates background jobs to make one way call. For example
[HttpPost]
public IActionResult DoWork(int id)
{
BackgroundJob.Enqueue<IWorkerService>(x => x.DoWork(id));
}
So if i create console application just for recurring jobs, i still have to configure Hangfire in API for background jobs. I am trying to avoid configuring Hangfire in two places.
What are my options in ASP.NET Core to make API always running?
UPDATE 1
based on discussion here i made the changes to App Pool settings
set Regular Time Interval to 0
Change Idle Time-Out from default 20 to 0
I will wait for few days to see how this settings works
However, i am not sure if its a good idea not to recycle app pool ever? Any suggestion

Ideally, you should follow the official docs.
In your case, you should also set the Managed pipeline mode to Integrated, the Start Mode to Always Running, the Preload Enabled to true, and also some edits in the Configuration Editor.
All these are mentioned and better detailed in the official docs (linked above).

Related

Run exe file alongside .NET Core Web Application

I have an executable file which I would like to run alongside a .NET Core web application. It needs to start when the web application loads and will continuously run until the web application ends. I also need to be able to check if the executable file is still running through the UI, and have an option to start, stop or restart.
I have done a bit of googling already on this and all that keeps returning is examples where the exe file is expected to end at some point, not anything that continuously runs.
Any pointers in the right direction would be great.
Andy's comment is correct. It's recommended to using Background tasks with hosted services in ASP.NET Core.
And you also can use signalr to record the status of your service. You can start, stop by using StartAsync and StopAsync. And it's impossible to restart. The background service was launched with asp.net core.
For more details, you can check the blog:
Communicate the status of a background job with SignalR

How to make sure no telemetry is lost when querying Application Insights in a Visual Studio (local debug session of ASP.NET CORE)?

I have an ASP .NET Core app with Application Insights added. I run it locally in debug mode, send in requests at a rate of 10 per second, then examine the telemetry. I see less than half of request telemetry events from ASP and less than half of trace events produced by ILogger inside my own code. I know about adaptive sampling, but thought that trace events are excluded from it by default? (seeing how applications logs become traces in App Insights). I tried adding explicit config to make sure no telemetry is lost, but so far it didn't work:
services.AddApplicationInsightsTelemetry((ApplicationInsightsServiceOptions opt) =>
{
opt.DeveloperMode = true;
opt.EnableAdaptiveSampling = false;
});
Can I somehow configure App Insights to gather 100% of telemetry in local debug?
Update after playing around with configuration and suggestions, I noticed that visual Studio debug session is limited to searching the last 250 events, and this is what deletes my telemetry.

start service from asp.net core web application

I am Building web application on asp.net core.
my application needs some data once in a week.
I have created console application which gets this data (really, it parses some website once in a week and stores that data in a database).
I have configuration file when should that console application start to get that data.
My question is how to start that console application from my web app and is it a good idea to start console application from web application?
IMHO it's not best practice to start a console application from an web application. If you would like to do that, please consider using a Windows Service with an timer or a cron job for that.
What you can do is create a scheduled tasks using Windows Task Scheduler. This task's action will be to State a Program and then you can set Program/Script to your console exe file. You can schedule this task to run once a week.
You can check this task history to ensure the task is executing weekly and can run it manually in case of failure.

WCF in webjob returns list of records to webapp

Is this possible to put WCF in webjob that will return list of records to webapp.Actually I have project that returns search results (searching is done via lucene.net). Is there any guide or way to get results in my webapp from webjob?
Also can anyone guide me on my localhost I am running my webapp and web job is part of same solution. When I run web application, main function of WebJob is not hitting. Web app and web job can run simultaneously? If these are not runs simultaneously then How can I invoke my searching project initially ? How can my web project relate with my web jobs? I know about invoking by queue but some functions should be run initially when web application is started.
I want to test this behavior on my localhost
Is this possible to put WCF in webjob that will return list of records to webapp.Actually I have project that returns search results (searching is done via lucene.net). Is there any guide or way to get results in my webapp from webjob?
As I known, Azure WebJobs provide you with an easy way to run scripts or programs as background process in the context of your Azure Web Apps. You could not get results directly from WebJobs in your Web Application, you need to store your results in a central data center (Azure Queue, Table Storage,Service Bus,etc.), then you need to retrieve the data explicitly in your Web App. Here is a official tutorial about web application working with Azure WebJob.
Also can anyone guide me on my localhost I am running my webapp and web job is part of same solution. When I run web application, main function of WebJob is not hitting. Web app and web job can run simultaneously?
You could right click your solution and select Properties, choose Startup Project under Common Properties, choose Multiple startup projects and configure the Action for your web application and your WebJob. For more details, you could refer to this issue.
UPDATE:

MVC slow if site has been idle

I have several MVC4 sites on my VPS. They all have the same issue. They are so slow (up to 10 seconds to load, which is not a good first impression).
I am aware that when you first load your site, it will take a little while due to various things I've read upon. I understand this.
The problem is the following situation is true.
Publish MVC4 site to server.
Load MVC4 site in browser a PC. Site takes a while to load but expected.
Look at other pages on the site. A few are slow, but generally all is OK.
Close the website.
Reopen the website.
All pages respond well.
Close the website.
Leave it idle for 1 hour.
Open website, it is slow again, as if it's only been published
I can only assume it's something to do with the session, IIS or the app pool
In webforms I used to have tracing, I don't appear to have that in MVC and I don't know how I can debug such an issue.
That's actually the expected behavior, given that after a period of inactivity the application pool will recycle, to free up unused resources. Check the application pool's Recycle settings to configure that duration.
If you also want additional control on what happens during recycling there are a few options:
IIS7.5 (ASP.NET 4) supports a feature called "AutoStart", which can warm up your site after deployment and after recycling.
IIS8 has additional support through the Application Initialization Module.