I've had a lot of people say that the Service Loader I created is great, but that they would like the ability to run the service without a debugger attached, like from the command line or similar method, but still not require the service be installed as a Windows Service. 

I created a new application as an enhancement to the original Service Loader called the Service Runner.  What is beautiful about this is that it requires no code changes whatsoever to the service to run it without installing it.  Not even the one line code change described in the original Service Loader article is necessary.  Here's how it works.

  1. When you first run the program, it will ask you to select an assembly to find services in:

    image

  2. If the application cannot find an app.config file automatically (it looks for <AssemblyName>.config just like the CLR does by default), it will allow you to specify one.  If you do not need a config file, just hit cancel:

    image
  3. If everything checks out, you'll see the old familiar Service Loader interface with Start, Stop, Pause, Resume, etc:

    servicecontroller

That's it.  Note again that this required no code changes to the service to work.

There is a small caveat to this, however.  None of your Main() function code (if you have any you care about) will execute.  It's not very common to have any setup code in your Main() function for a service, but I know someone will email me about this so I just wanted to mention it.

Design Considerations

I was not comfortable with using the original Service Loader to allow execution outside of Visual Studio because it was designed as a debugging aid only.  I wanted there to be no way it would accidentally execute when deployed.   Any technique I could come up with for doing this was too invasive, risky, or hard to use.

I decided to create a service runner application that would use the same techniques that the Service Loader did, but be outside of the application.  Basically it loads the assembly you chose, finds all of the ServiceBase types and manually loads them into the Service Loader.  Even if you select an executable, it doesn't actually execute it - it uses it just like a library.

Because it's more of an outside-looking-in solution, it has even less risk of affecting the application once it is deployed, but still gives you the flexibility of running the service without first installing it.

Download

I've updated the original Zip files to include the new source and executable.  Just double-click ServiceRunner.exe and you should be able to run any .NET Windows Service.

Service Debugging Helper (Binary)
Service Debugging Helper (Binary + Source)

Bugs?  Contact Me.

Trackbacks

No Trackbacks

Comments

Peter Müller :

Thank you very much! This is very helpful.

Link

Stéphane :

Just wanted to say thanks! Your work is going to save me a lot of time in an urgent project.

Link

Simon :

I was just thinking to myself "wouldn't it be a great idea if...", and you've already done it.

Works great. Thanks a lot.

Link

Pedro :

THANKS a bunch!!! Just wanted to say that :)

Link

marco :

thanks, it is running :)

Link

Andreas Saurwein :

Great tool, but two things:

1) its missing the "Attach Debugger" button

2) if the service calls SetServiceStatus(), or RequestAdditionalTime(), one gets an exception, since those can be called only under the service process.

Link

Anderson Imes :

Andreas,

Both very good suggestions.  I will get right on #1: why didn't I think of that?

As for #2, that's going to be a rough one to intercept.  I'll investigate.  I wanted to avoid sort of "replicating" the windows services environment, but to get those two methods to work, I'd have to do a little bit of that.

Link

Andreas Saurwein :

Right, as far as I could see, the ServiceBase.RequestAdditionalTime() actually calls also SetServiceStatus() which is a native function in advapi32.dll .

So I guess its getting quite hard to simulate. Maybe with some reflection magic one might redirect the calls to a simulated function in the service runner or something like that.

Lets see...

Link

Leave a Comment