0

I need to do some process on my Asp.net website every night at 12 a.m

My process includes executing some SQL query and deleting some files and folders and create new files and ets.

My programming language is C#

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mnshahab
  • 770
  • 7
  • 16

4 Answers4

1

Create a console application and run it as a Windows Scheduler task

Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
1

A web application isn't suited for this. Web applications are request/response systems, they're only "running" when they're responding to a request. Beyond that, they're at the mercy of the web server's resource management.

What you're looking for is either:

  • A Windows Service
  • A Console Application

In the case of a Windows Service you would implement it with a Timer to execute some functionality at specific times. In the case of a Console Application it would execute that functionality by default and would be scheduled to run at specific times (likely using the Task Scheduler built-in to Windows).

If the functionality you're looking to execute is part of the web application, that's alright. You can extract it from the web application into a Class Library in the same solution and have the web application reference that library. Then add the Windows Service or Console Application to the same solution and reference the same library. That way they'd be sharing the same code.

On a note of personal preference, if you're not familiar with Windows Services very much then you'll likely find that a Console Application will be much easier to debug.

David
  • 208,112
  • 36
  • 198
  • 279
1

As a server-side task I would not recommend a console application at all.

The best thing is you use a Window Forms application, remove all code that creates the initial form and the references to the Windows Form API.

What you are left with is an application that will not open a console window, not open a form or anything else. It will just run without any GUI.

There you can put in everything you want.

Then schedule the application to run at the specified time: Task Scheduler

This is far better because a system administrator can change the scheduling without having to touch your code.

pid
  • 11,472
  • 6
  • 34
  • 63
1

While not the most sexy solution. By far the easiest solution would be to hit an end point in your application using a scheduled task there by triggering the functionality you want to run at your allotted time. I have seen this deployed quite successful in fairly large applications.

A good description of how you can do this is found in the following post.

Recommended method for loading a URL via a scheduled task on Windows

For all those advocating building services etc. I agree that that would be a good solution but if all you want to do is run a quick batch job once a day perhaps simpler is better.

Community
  • 1
  • 1
SzabV
  • 244
  • 1
  • 6