2

I am trying to run a background service which just writes to a file on a specified interval.
There are two methods that I tried
1) Created the project with the Console application template
2) Created the project with Web Application as template

When I run the app from visual Studio, both of them run fine. But when I deploy them to IIS, only the web application version works. It must be noted that there is absolutely no difference between the code of the two projects. I have used the WebHost as a hosting strategy in both the projects as well as well as installed all the dependencies in case of Console application as there are in the Web Application version.

I must also inform that I have used the preloadEnabled="true" option in IIS as IIS needs a web request to start the application.

I am wondering what is the difference between both the project types as the code is the same? I don't want the Web Application template.

Edit 1: I forgot to mention that the service will also need to expose an api endpoint for healthcheck purposes. Will the windows service approach listen for http requests?

I used the following article for implementing my background service. https://learn.microsoft.com/en-us/dotnet/architecture/microservices/multi-container-microservice-net-applications/background-tasks-with-ihostedservice

7
  • 1
    If you want a service project, there is a (relatively) new template for that: dotnet new worker - that should fit your requirements. Commented Oct 25, 2019 at 11:15
  • 5
    If you're not building a web application, why are you involving IIS at all? Write it as a service instead. Commented Oct 25, 2019 at 11:15
  • I agree, why would you even think about doing that as a WebThing? What is the work? Why are you doing it? Are you preparing a file to be stored into the DB, like re-encoding a image? Because that is about the only case where I could think it being valid. Commented Oct 25, 2019 at 11:18
  • 4
    "On a specified interval" suggests that you simply create a console application and run it using Task Scheduler. Windows Service is for continuously running processes. Commented Oct 25, 2019 at 11:18
  • 1
    To answer the question - asp.net core web apps ARE console apps. But this does not mean that a console app would run out of the box. Commented Oct 25, 2019 at 12:00

2 Answers 2

4

After years of building background services, I learned that Windows services are the best tools to implement these applications. While there are different techniques to keep an IIS application up and running in the background and prevent it from getting recycled, in practice, the applications on IIS are not meant to be executed forever.

If you had an intention to build your app in the cloud, I would have suggested using something like Azure WebJobs or Azure Functions Timer-Triggered functions, but for on-premise, even using something like Hangfire in the web is not sustainable. The worst happens when you need backward compatibility on Windows servers that don't have the "Application Initialization" module.

My suggestion is to move your application to a simple Windows Service if you control your environment. Windows services consume less memory, are easier to manage, and can run forever without getting recycled.

Sign up to request clarification or add additional context in comments.

6 Comments

I usually advise against Windows Services as they are a pain to develop and debug. Not having any easy way to talk to the user is a big problem. I just recently realized that Hello World is the first code for a reason - it shows you how to get random Messages out of your code for debugging :)
I also have another requirement that the application will expose an api endpoint for healthcheck purposes. Will the background service be able to process http requests?
I still didn't get the answer to my question. Is there any difference to the two approaches I mentioned? It appears to be something under the hood that I am unaware of. I am not able to find what exactly is that.
I think for the second use case, you need to create a simple web API application to do the job. I know it seems to be a lot of work, but think about it this way: do you build a game on top of Windows Services? You could, but it's not designed for that purpose.
On Windows service vs. console, remember that Windows services are getting started automatically. You have to run your console application manually, and you need to have active logged-in users to be able to run them in the background.
|
1

WebApplications are plain the wrong tools for this.

Being always on and always reachable, WebServers are primary targets for hacking. To compensate for that, they are usually run under the most restrictive user rights you can imagine: Read rights to their programm and this instances content directory. While I do not know why it worked at all, it propably will stop working in Production.

What you wanted to write was eitehr a Service or something executed by the Windows Task Sheduler. Personally I advise for the Task Sheduler as Services have their own set of restrictions. Unless of coruse there is some detail of the requirements that you did not told us.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.