Am I Running as a Service

Am I Running as a Service

Like Ash, I write all actual processing code in a separate class library assembly, which was then referenced by the windows service executable, as well as a console app.

However, there are occasions when it is useful to know if the class library is running in the context of the service executable or the console app. The way I do this is to reflect on the base class of the hosting app. (Sorry for the VB, but I imagine that the following could be c#-ified fairly easily):

Public Class ExecutionContext
''' <summary>
''' Gets a value indicating whether the application is a windows service.
''' </summary>
''' <value>
''' <c>true</c> if this instance is service; otherwise, <c>false</c>.
''' </value>
Public Shared ReadOnly Property IsService() As Boolean
Get
' Determining whether or not the host application is a service is
' an expensive operation (it uses reflection), so we cache the
' result of the first call to this method so that we don't have to
' recalculate it every call.

' If we have not already determined whether or not the application
' is running as a service...
If IsNothing(_isService) Then

' Get details of the host assembly.
Dim entryAssembly As Reflection.Assembly = Reflection.Assembly.GetEntryAssembly

' Get the method that was called to enter the host assembly.
Dim entryPoint As System.Reflection.MethodInfo = entryAssembly.EntryPoint

' If the base type of the host assembly inherits from the
' "ServiceBase" class, it must be a windows service. We store
' the result ready for the next caller of this method.
_isService = (entryPoint.ReflectedType.BaseType.FullName = "System.ServiceProcess.ServiceBase")

End If

' Return the cached result.
Return CBool(_isService)
End Get
End Property

Private Shared _isService As Nullable(Of Boolean) = Nothing
#End Region
End Class

Detect if code is running as a service

You can add the Microsoft.Extensions.Hosting.WindowsServices NuGet package and use the WindowsServiceHelpers.IsWindowsService() helper method.

For Linux you can use Microsoft.Extensions.Hosting.Systemd and the SystemdHelpers.IsSystemdService() method.

How to test whether a service is running from the command line

sc query "ServiceName" | find "RUNNING"

Determine if running as VCL Forms or Service

I actually ended up checking the application.showmainform variable.

The problem with skamradt's isFormBased is that some of this code is called before the main form is created.

I am using a software library called SvCom_NTService from aldyn-software. One of purposes is for errors; either to log them or show a message. I totally agree with @Rob; our code should be better maintained and handle this outside of the functions.

The other intention is for failed database connections and queries; I have different logic in my functions to open queries. If it is a service then it will return nil but continue the process. But if failed queries/connections occur in an application then I would like to display a messaage and halt the application.

How do I detect that my application is running as service or in an interactive session?

I think you can query the process token for membership in the Interactive group.

From http://support.microsoft.com/kb/243330:

SID: S-1-5-4

Name: Interactive

Description: A group that includes all users that have logged on interactively. Membership is controlled by the operating system.

Call GetTokenInformation with TokenGroups to get the groups associated with the account under which the process is running, then iterate over the sids looking for the Interactive sid.

I found a nice chunk of code at http://marc.info/?l=openssl-dev&m=104401851331452&w=2

C#/.NET: Detect whether program is being run as a service or a console application

Rasmus, this is the earlier question.

From the answers it seems the most popular way is to use a simple command line option, or try accessing the Console object in a try catch block (in a Service the Console is not attached to the process and trying to access it throws an exception).

Or if you're having trouble testing/debugging the service, move code into a separate dll assembly and create a seprate test harness (winforms/console etc).

(Just noticed that Jonathan has added his solution to the end of the question.)

Is it possible to verify if a Windows Service is running from a different computer?

You can do this with WMI, this example works for me (Obviously comment out the Stop part)



Related Topics



Leave a reply



Submit