Maybe you find the need to; or you think of the possibilities of controlling a Windows Service from a Web App might be useful. For me I like the idea of writing a Windows Service that does some server function but can be controlled or called by a user. This is a more detailed post on code examples from the previous post with some other considerations.
- Maybe a Administrator needs to start and stop services but doesn’t have access to the server by normal methods.
- A user that calls a Windows Service that performs some type of Emailing functionality such as a notifying users when they have a new Task. Maybe this table is populated by many means. Win service would check the database every 30min and send off emails. In your web app, your users are special, so every time they assign a task, you fire off the windows service right there and then. Or maybe sometimes you have the need to fire off a manual send and have written redundant code to do it.
- Windows service control center for developers? Or Support users?
Anyway, the process is quite easy.
Windows Service code, somewhere have a method like this…
- Protected Overrides Sub OnCustomCommand(ByVal command As Integer)
- Select Case command
- Case 130
- 'Call one of your other methods from here
- Case 150
- 'Call some other method or command
- Case Else
- 'Do Nothing
- End Select
- End Sub
You can only call one CustomCommand, so you send in Command Integers and from there decided what you want to do.
In your ASP.Net app on a button click event..
- Using sc As System.ServiceProcess.ServiceController = New System.ServiceProcess.ServiceController()
- sc.MachineName = "Mastro-PC"
- sc.ServiceName = "ComplyTrackMainTimer"
- End Using
This will work on a web.app on a button click event. You have to set the MachineName and the ServiceName. You can also Stop() and Start() the Service or look up the Status.
You can get the MachineName of the server by opening a command prompt from the server and typing in “HostName”
You can get the ServiceName by opening up the server and looking at the name in Services. Don’t use the Display Name use the Service Name.
I have a previous post on this with more detailed information on valid Command Integers and Security.