Search code examples
c#azurebackgroundworkerazure-worker-roles

Separate Worker Role vs Windows Service alongside Another Role


Windows Service Applications are useful to create long running applications that have their own sessions. They are useful to run in the background without interfering the main (or line-of-business) applications. The way to run these applications on Azure can be done alongside the actual roles by having start up task definitions (see 1, 2).

On the other hand, Worker Role itself is working in the background all the time just like long-running tasks. On top of RoleEntryPoint definition the start, stop and running processes can be handled easier I think through (OnStart(), OnStop() and Run() methods). In fact, the role provides an ability to update live configuration as well. Plus, performance metrics are given. Similarly you can run worker roles in elevated mode.

In that respect, if we compare running a background task over a Windows Service alongside another role versus a separate Worker role, the latter approach seems advantageous. Still would there be any reason to go with the former? If so, what is the advantage?

Let's say the task here is monitoring. Background task would collect ETW events outputted by the application running on other roles and save them into a persistence storage.


Solution

  • The two are not mutually exclusive. You can still install Windows Services, even in a worker role (especially if the software id designed specifically to run and be managed that way). To do that in a worker (or web) role, you'd just need to bundle up (or download) the appropriate installer to set your service up.

    The worker / web role scaffolding itself lets you run pretty much anything in your Windows VM. Don't think of it as a background task. You have the full VM at your disposal. You need to get things set up, through startup cmd script and/or OnStart(). And you need to manage lifetime of the VM instance (to prevent rebooting) by dealing with Run(). You can spin up threads, set up parallel tasks, whatever.

    Now: Whether you choose to write new services or just rely on web/worker scaffolding: that's up to you and your particular app, and I don't want to make this a subjective/opinion-based answer. :)

    One more thing: When you're building out Windows Virtual Machines (and not the web/worker type), where you don't have the scaffolding of the role instances, Services might be more valuable in that environment.