Is there a way for an .NET library to detect whether or not it is being run as a service?
My library can be run either way. But when its run as a service, developer
There isn't really any way to tell if your library is running in the context of a service or not though you can use Environment.UserInteractive to make a guess.
But generally a library should never depend on its application context. A library should provide services to an application and if it requires different parameters depending on how it is called it should require the application to provide those parameters.
Your library probably does not act differently strictly based on whether or not it is hosted within a service but rather there is some information about the service environment or user that your library needs to be informed of. The application should inform the library of the necessary conditions or information, the library should not guess on its own.
EDIT: I'd upvote the comments on your question if I could. Use overloaded methods if necessary and/or simply fail if all the information necessary is not provided.