views:

57

answers:

4

Hello We are developing a Windows Presentation Foundation Application that we would like to be able run as a Windows Service.

Anyone done something like that?
Is it possible?

We don't need to interact with the Desktop or any GUI, it would just be nice to have one App that we can run with a GUI, from the Command Line (that works) or as a Service.

Looking forward to interesting input :-)

+3  A: 

As a rule of thumb services should never have any kind of UI, this is because services usually run with very high privileges and can bad things can happen if you are not super careful with your inputs (I think the newest versions of windows won't let you do it at all but I am not 100% sure). if you need to communicate with a service you should use some form of IPC (wcf, pipes, sockets, ect...). If you want a simple console program that also can be a service I know of a trick set that up.

class MyExampleApp : ServiceBase
{

    public static void Main(string[] args)
    {
        if (args.Length == 1 && args[0].Equals("--console"))
        {
            new MyExampleApp().ConsoleRun();
        }
        else
        {
            ServiceBase.Run(new MyExampleApp());
        }
    }
    private void ConsoleRun()
    {
        Console.WriteLine(string.Format("{0}::starting...", GetType().FullName));

        OnStart(null);

        Console.WriteLine(string.Format("{0}::ready (ENTER to exit)", GetType().FullName));
        Console.ReadLine();

        OnStop();

        Console.WriteLine(string.Format("{0}::stopped", GetType().FullName));
    }
    //snip
}

if you just start the program it will launch as a service (and yell at you if you run it from the console) but if you add the paramter --console when you start it the program will launch and wait for you to hit enter to close.

Scott Chamberlain
Why the downvote?
Scott Chamberlain
This won't work very well as a windows service because it doesn't handle the OnStop event. You won't be able to shut down the service from the Windows Services list.
Spencer Ruport
@Spencer Ruport My service has a OnStop section. I cut it down (twice) to make my point more clear, the OnStart and OnStop are inside the `//Snip`
Scott Chamberlain
This is fine for the CLI and Service.
Steven Sudit
For the record I didn't downvote you!
Spencer Ruport
No hard feelings, whoever did it took it back. :)
Scott Chamberlain
Thanks for the code. Have to dig a little deeper into the Services Stuff to understand the OnStop part.
Remy
A: 

If you already have it working from the command line you're half way there. Some apps just have simple flag designations you use when you want to run the app as a service, check for these flags and have your logic flow accordingly. Something like:

myapp.exe --runservice
Spencer Ruport
Not bad advice, but I find it's usually better to have the CLI run as a service by default. It simplifies setup.
Steven Sudit
A: 

Check this out. This makes any executable run as a service.

English : http://support.microsoft.com/?scid=kb;en-us;137890&x=7&y=13

French : http://support.microsoft.com/kb/137890

David Brunelle
A: 

Don't do this. Write all the guts in an engine assembly, then reference it from the GUI and the service. The CLI can either be a third executable, or a reuse of the service executable. The key is to allow the service to run without any references to WPF or WinForms or whatever GUI framework you use.

Steven Sudit
I was thinking about this too. Sounds like a resonable solution. But why is a reference to WPF or WinForms bad? As long as it is just a reference?
Remy
@Remy: A static reference loads the assembly in at the start.
Steven Sudit