简体   繁体   中英

Run a WPF Application as a Windows Service

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 :-)

As a rule of thumb services should never have any kind of UI. This is because services usually run with very high privileges and bad things can happen if you are not super careful with your inputs. (I think the newest versions of Windows won't let you create UI from a service 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, ...). If you want a simple console program that can also be a service, I know of a trick to 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.

We actually worked it out and wrote a little post about it:
http://remy.supertext.ch/2011/11/a-wpf-project-running-from-the-cmd-prompt-or-as-a-service-and-has-a-gui/

Maybe it helps someone.

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.

My solution is to add a Main entry function, like:

public static void Main()
{
    ServiceBase.Run(new MyService());
}

Now we have two app startup entries - one for WPF and one for Windows Service.

We can configure the startup object in Visual Studio by right-clicking on your project and selecting Properties -> Application -> Startup object .

Setting the entry and rebuilding will get you what you want.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM