views:

334

answers:

6

Hi,

I need to create a service in .NET that maintains (inner) state in-memory, spawns multiple threads and is generally long-running. There are a lot options -

  • Good-old Windows Service
  • Windows Communication Services
  • Windows Workflow Foundation

I really don't know which to choose. Most of the functionality is in a library used by this service, so the service itself is rather simple.

On one hand, it's important the service host is as close to "simply working" as possible, which excludes Windows Service. On the other hand, it's important that the service is not taken down by the host just because there's no external activity, which makes WCF kind o' "scary". As for WF, it's strongest selling point is the ability to create processes as, um..., workflows, which is something I don't need nor want.

To sum it up, the plethora of Microsoft technologies got me a bit confused.

I'd appreciate help regarding the pros and cons of each solution (or other's I've failed to mention) for the problem of a stateful, long running service in .NET

Thanks,
Asaf

P.S.,
I'm using .NET 4.

EDIT:

  • What I mean by the host "simply working" is, for example, that the service I create be reactivated if it crashes.
  • I guess the reason for this question is that I've created Windows Services in the past (I think it was in plain C++ with Win32 API), and I don't want to miss out on something simpler if there's is such as thing.

Thanks for all the replies thus far!
Asaf.

EDIT 2:

I'll use a Windows Service, and might host a WCF service inside it to allow other processes to communicate with it.

Thanks,
Asaf

+5  A: 

Based on your description, your best bet is #1, a Good Old Windows Service. You can assign work to it, it can run as long as you want, and it can spawn threads. Not sure what you mean by "simply working," or why that puts a Windows Service out of the running.

You could, alternatively, make a simple console app, but you'd have to handle the management (restarts, etc) yourself.

WCF is essentially .NET remoting over the web, it doesn't handle long-running processes by itself. Workflow Foundation can handle long-running workflows, but only that.

Dave Swersky
A: 
  1. as "simply working" as possible - which excludes Windows Service...
  2. not taken down due to inactivity... scary - WCF...
  3. no need of a workflow, nor want it - Workflow Foundation...

It seems there's not as much as options as you would like to believe it! ;P

  • What about a simple EXE that does the job, and that is scheduled through the Windows Task Scheduler? You manage the multithreading from inside your app. This would be as simple as a console application.

EDIT My first choice would be a Windows Service as well, but it is simpler through the scheduler.

Will Marcouiller
+1  A: 

According to your requirements "(inner) state in-memory, spawns multiple threads and is generally long-running" there are really two options:

  1. Console application
  2. Windows service

Other two mentioned are derivatives of these. First one is too primitive and let's forget about this. But for ease of debug i recommend in Main check Environment.UserInteractive and if true run as console app, else as service. So your choice should be 2nd.

WCF is communication framework and has nothing about long running service. IIS can host WCF apps, but no multithreading and inmemory data there, it is single call as web service. WF is workflow framework and again has nothing about services. It can help implementing complex flow logic.

Both WCF and WF can be used in Windows service.

Andrey
+2  A: 

Since you don't seem to need any workflow-related features, my vote goes out to Durable Services in WCF. This is new in .NET 3.5, and it allows WCF serviecs to persist their state into a provider-based "persistance store", typically a SQL Server backend database (but there's also a filesystem-based provider, and it's extensible - you can write your own if need be).

Check out some excellent blog posts on this topic:

WCF is a great and powerful communication library which frees you from a lot of hassle and details that you'd have to deal with when creating all of this from scratch.

WCF can be hosted in IIS (with all its deficiencies), or you can stick your WCF service into a Windows NT Service and have it start up and run when the machine boots, without anyone being logged on.

marc_s
Your first link says that "WCF Durable services are WCF services in which the operations can remember the values of private variables (=the state of the service) inbetween restarts of the serivcehost and/or client.", which is interesting, but not what I need. I did learn something new. Thanks!
Asaf R
A: 

Maybe check out Quartz.Net? Its more of a scheduler but you can start jobs etc on multiple threads. I used this instead of a Windows Service with great success.

Kenny Eliasson
+1  A: 

I would suggest the good ol windows service. I have written many many services using C# for these very purposes. They can spawn threads, run for an eternity, and are well designed to do what you want. You can create a .net windows service right in Visual Studio. It's one of the .net project types.

WCS and WCF will both do what you want but require some extra work and really weren't designed for what you seem to want to do.

Justin