5

I have a .NET 4 web application that has 3 separate projects associated – DAL, BAL, and UI. I am using Entity Framework for database interaction.

I have code that cycles through a bunch of database data, calls methods depending on what it finds, and then updates a database. I want this code to run all the time. At the same time I want users to be able to log in and run reports etc all while the code in the background is constantly running.

What is a good approach to this? Do I create a service for the code that constantly runs, a separate thread, an entirely separate project for the code that runs constantly, or a different approach..?

Also, depending on the answers given, how would I kick start the code that runs constantly? i.e. just through form load method or is there a better approach? I currently kick start the code by clicking a start button; this is fine for testing, but not going to work when in production.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders Aug 13 '12 at 14:47
  • If the thread's only purpose is database interaction, and the DBMS is SQL Server, then you might want to consider implementing it as a [.NET assembly hosted in SQL Server.](http://msdn.microsoft.com/en-us/library/ms254498.aspx) – mbeckish Aug 13 '12 at 14:49
  • @mbeckish OP is using **Entity Framework** - Entity Framework is not supported in .NET CLR. See [related SO post](http://stackoverflow.com/a/2794127/175679). It would have to be converted to straight ADO.NET. – SliverNinja - MSFT Aug 13 '12 at 14:53
  • @SliverNinja - The OP does not specify that the background thread needs to use Entity Framework. Maybe just the DAL part of the ASP.NET site should use Entity Framework. – mbeckish Aug 13 '12 at 15:25

4 Answers4

5

You would be best suited for using Windows Services for always-running tasks.

Running code on a separate thread under IIS is not a reliable mechanism since IIS can terminate threads at will to conserve server resources.

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • 2
    You can set your service to automatically start as well. Also ensure that your accessing code handshakes and ensures the service is started before the magic happens. – crlanglois Aug 13 '12 at 14:52
  • Does creating a Windows Service affect web hosting options. Say i want GoDaddy to host my application. Can i install a windows service on their web server? – user1595656 Aug 13 '12 at 14:59
  • If you are using GoDaddy, you would have to host the service remotely - see [related GoDaddy forum post](http://support.godaddy.com/groups/web-hosting/forum/topic/scheduled-task-or-windows-service/). You should look at migrating to [**Windows Azure**](https://www.windowsazure.com/en-us/home/features/cloud-services/). Setting up web role + worker role + SQL DB under Azure would solve this issue for you. – SliverNinja - MSFT Aug 13 '12 at 15:11
  • Windows Azure is not an option right now. If possible, i would like a solution that will work with what i have currently. – user1595656 Aug 13 '12 at 15:13
  • For reliable behavior you would have to run your windows service remotely or switch to [GoDaddy physical/virtual dedicated servers](http://www.godaddy.com/hosting/virtual-dedicated-servers.aspx?ci=13454) to get admin level access to the remote machine and install your service. – SliverNinja - MSFT Aug 13 '12 at 15:16
  • Could you expand on this. Do you mean I personally host my Windows Service out of my house and have my web application that is hosted by a company like GoDaddy call the Windows Service? I am probably missing something here. – user1595656 Aug 13 '12 at 15:20
  • Close - you host the windows service outside of GoDaddy. The service calls into the database via EF (*assuming you can access your SQL DB externally*) or it could call pages/methods on your web application (*WCF, Web API, etc.*) – SliverNinja - MSFT Aug 13 '12 at 15:22
  • On any windows machine you own or have access to. – SliverNinja - MSFT Aug 13 '12 at 15:54
  • I appreciate your suggestions. I really need the security/reliability of a hosting company. I understand you can't run a windows service on a shared hosting environment but are there really no other options than a window service if you want to run code continuously? I don't know too much about WCF.. is that an option? Something besides a Windows Service. – user1595656 Aug 13 '12 at 15:59
  • If you cannot use an actual windows service, you can [try simulating one using HTTP Cache](http://stackoverflow.com/a/1135373/175679). However - this approach is nowhere near as reliable since it's based on IIS. You may want to reconsider your hosting options & configuration. – SliverNinja - MSFT Aug 13 '12 at 16:04
  • You provided me with a lot of insight and the answer by Ragoczy was useful as well. Because you stuck with it I marked your answer as correct and upvoted Ragoczy.. This is my first time doing this so if i made a mistake please correct me. – user1595656 Aug 14 '12 at 14:22
2

Given your question and clarifications on other answers that:

  1. Your solution runs in a hosted environment where you cannot install a service;
  2. Calling it from a third server (i.e. Azure or such) is not an option for you;

You might be best off starting a thread in your Application_Start event to manage the database work. You'd probably want to ensure that this thread had some periodic idle time, so as not to take up too much of the hosted environment and ensure it's shutdown when your application ends or is restarted.

A service would really be optimal, but if you're in a hosted environment and can't/won't use another server, then that's not possible.

Ragoczy
  • 2,907
  • 1
  • 19
  • 17
0

Use a Windows Service. Should also look into using Stored Procs for the database interactions you mentioned. In terms of kicking the Windows Service off, you can set it to automatic startup (when the OS starts) which will mean it will run until terminated.

Mark Walsh
  • 3,241
  • 1
  • 24
  • 46
0

I would only recommend a Windows Service if it will literally always be running. However, "always" usually means every x seconds / minutes / hours /days.

If x is greater than a few minutes, I would make it a Console Application and run it through the Windows Task Scheduler. This way you don't have to worry about memory leaks and a slew of other issues.

However, if it is only working with the database, I would recommend a stored procedure and a Sql Job.

cadrell0
  • 17,109
  • 5
  • 51
  • 69