简体   繁体   中英

Convert Windows Application to Windows Service

I am having a Windows Application and I want to run it with WINDOWS SERVICE. I have created a WINDOWS SERVICE Application, now How would I integrate my WIN APP within this windows service application?

This is quite a common requirement and I suggest you to consider the following: My code will be be using the following package : TopShelf

After :

nuget Install-Package Topshelf

In your start have something like the following :

    public static int Main()
    {
        var exitCode = HostFactory.Run
        (
            c =>
            {
                c.Service<Service>
                (
                    sc =>
                    {
                        sc.ConstructUsing(name => new Service());

                        sc.WhenStarted((service, hostControl) => service.Start(hostControl));

                        sc.WhenStopped((service, hostControl) => Service.Stop(hostControl));
                    }
                );

                c.SetServiceName("ServiceName");

                c.SetDisplayName("DisplayName");

                c.SetDescription("Description");

                c.EnablePauseAndContinue();

                c.EnableShutdown();

                c.StartAutomaticallyDelayed();

                c.RunAsLocalSystem();
            }
        );

        return (int)exitCode;
    }

And follow the configuration available in TopShelf configuration

We are using TopShelf in lots of our projects and it completely fulfills our needs.

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