Given a C# console application that will run as a windows service, there are sort of 2 application entry points. The first obvious one is the static void Main
method. Part of the job of this method is to install something that extends ServiceBase
and then run it to get its OnStart
method to be invoked. There are also higher level tools like Topshelf that help you avoid lower level ServiceBase
implementations, but still, you end up with 2 potential application entry points: static void Main
, and some kind of OnStart
method.
Should the application root for such a service be composed within the service's OnStart
method, or earlier than that as part of static void Main
?
It seems that there could be an advantage to composing within the service's OnStart
method (and destroying / disposing it in the OnStop
method) since restarting a service would then compose a new application root. The only real disadvantage I can see here is that if I am using a tool like Topshelf, I can't use the DI container to obtain an instance of my Service class. Then again, that's probably not a real disadvantage. Still, most of the applications I read compose the root during Main
, not during OnStart
, and I'm not sure why.
Is one approach really better than the other, or does it depend, and my question is really an opinion-based one?