简体   繁体   中英

When #if SERVICE does work and when it does not?

I often used to use (and as far as I can remember it was working just fine)

        public static void Main() {
#if SERVICE
            ServiceBase.Run(new MyService());
#else
            ....
#endif

in services code so I can run it as console or register with installutil and run as service.

But... since some updates or with switch to 4.5.1 ( or even maybe in release build ) it just doesn't work anymore. Whenever I use installutil or just run as console it's using code for console, eg not for SERVICE and it fails because I used Console.ReadKey there and sure service can't deal with it.

Maybe I am doing something wrong and there is alternative ways of doing it?

... so I can run it as console or register with installutil and run as service

That can't have been enough. The #if SERVICE is a compile time condititon, it cannot produce different behaviours depending on how you run it. It needs a different Build configuration.

My guess is that you lost a piece of tooling or forgot a certain habit after that upgrade.

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