start service from asp.net core web application - asp.net-core

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.

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

Hangfire recurring job stops after app pool gets recycle

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).

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:

VB.Net running as a service

I have built an application that connects to the exchange server and does some scanning tasks on the incoming mail.
Application has a form where i can start and stop scanning,give in an interval, do some other configuration, and a notificationicon in the taskbar to show hide,end and so on..
The problem is that I would like the application to start as a service, because now a user has to log in first on the server before it starts working.
I tried some tools like RunAsService, i had a service, but obviously it didn't run the application.
Of course i understeand now that when the application will run as a service, scanning will be the only thing that will happen, no forms and no notification icons.
However i do not know where to start, should i extract the scanning logic from my configuration as a separate application? What application type should it become then ?
You have two easy options.
First one just create a new Windows Service Project from your New Project button in the Visual Studio, and then just program your app.
Second one, just create a service from the service management window in your computer and add the executable file of your program. It will start running as a service (with visible windows and everything as you coded it).
You should follow this link and schedule your vb.net windows program as windows task
In Your code, at the end ,when everything gets executed you should write Me.Close() to hide your form.

.net make console exe hidden?

I'm using Windows Task Scheduler Wrapper to schedule the tasks (basically to launch an exe console every two minutes or so) in a .net desktop application.
However, I don't want it to pop-up the console every 5 minutes or even once.
How do I hide it?
I understand if I was using vb.net to launch the exe, there are several things I could have done. However, I am using windows task scheduler to launch it. What do you suggest in that scenario?
The console app will only display if scheduled to run under your account. Simply create a new account, and schedule it to run under that account.