3

I was wondering if it's possible to loop some code while your web project is up (I'm using MVC4 RC and C#). For example you have an entity Factory. This factory creates a chair every hour. So the project should do this while it's running on the server. No matter if users are on the website. Is this possible? And how would you do this?

Nicholas
  • 1,189
  • 4
  • 20
  • 40
  • Maybe you want something like this: [ElapsedEventHandler](http://msdn.microsoft.com/en-us/library/system.timers.elapsedeventhandler.aspx). – chaliasos Jun 19 '12 at 19:34

2 Answers2

3

Yes there is a way to do this: put something in the cache with a specific lifetime and register with the OnCacheRemovedCallback. In this callback do what you want to do and insert a new item to the cache. You need to insert6 on startup to start the chain. This is done by SO (see https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/), so it works. It obviously works and may be called a pragmatic solution.

Some will consider this somehow 'hackish' and I would tend not to do this without knowing exactly what you do or why you want to do this. It is definitely not the fine art of doing such tasks.


Another option (user1308743 has mentioned this): Create a exe that does what you need and let it be called from the Task Scheduler every hour.


A slight different option: Write a windows service and implement a timer or use something like Quartz.NET to schedule your job this way. Maybe the solution with the most work and the most enterprise like, but it works (have implemented something like this and works like a charm for what I need to do). As an additional value, you can write your schedule in a way that you can make configurable.


Found another option on SO: Recurring tasks in ASP .NET

Community
  • 1
  • 1
Sascha
  • 10,231
  • 4
  • 41
  • 65
  • Interesting, and isn't too hard to implement. – formatc Jun 19 '12 at 19:40
  • Thanks for your answer, I will try the first solution soon. For the second one and third I have another question. I'm not really an advanced developer but when I'm using shared hosting I doubt I will be able to run / schedule a .EXE , or is this possible from the code? – Nicholas Jun 19 '12 at 20:30
  • I would like to try the first solution but was thinking about how to call the Create action of my factory controller. Searched the web for WebRequest and found something like the following WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html"); Could I call the 'Create' URL or is there another way to call the ActionResult? – Nicholas Jun 19 '12 at 22:28
  • In a shared hosting environment I doubt you can use a locally installed exe or service. Option 4 (running on your infrastructure and calling the URL) is still valid. – Sascha Jun 20 '12 at 04:28
  • You are then actually running in the web context - it would be rather senseless as you would stress the server where it's not required. You would not need to call a second time an url. You would do the stuff you're doing in the entity factory in this method. – Sascha Jun 20 '12 at 04:31
  • I'm sorry, I lost you somewhere there. With option 4 you mean the 'Recurring tasks in ASP.NET' subject of are we talking about the WebRequest thingy? – Nicholas Jun 20 '12 at 08:17
  • Yes. Basically you would call an URL from your machine that then does what you want to do. – Sascha Jun 20 '12 at 08:21
2

This is probably not the proper way to do this, but you could create a EXE that handles your task and leave it running on the server.

Kyle
  • 32,731
  • 39
  • 134
  • 184