How do I control Windows service application from external program?

Hello,

I hav a task of creating Windows service which has some GUI for configuration data used by service. So I plan to have 2 projects in solution - Windows service itself and frontend manipulation program. How do I attach from within a program to Windows service and how do I call events inside Windows service to reread configuration file once changes are made?

Thanks,

G

[399 byte] By [ChanKaiShi] at [2007-12-25]
# 1

If you're using .NET 2.0 then IpcChannel is probably your best bet here as it is designed for inter-process communication (hence the name!). The documentation can be found at http://msdn2.microsoft.com/en-us/library/system.runtime.remoting.channels.ipc.ipcchannel.aspx

As an alternative you could look at using remoting on .NET 1.x, or named pipes (you'd have to P/Invoke the named pipe APIs, but you can pass the returned IntPtr handle to the FileStream constructor and use that to read/write data.

GregBeech at 2007-8-31 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2
Another alternative is to implement custom command handlers for the

service. Your GUI code can then send custom commands to the

service using ServiceController. This is stratight forward,

but it is one-way only (from GUI to the service) mechanism, and if you

need the reverse direction communication, you'll have to implement it

separately like the IPC mentioned above, files, etc.

Dasa at 2007-8-31 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3

You need to work with ServiceController class to do this, SQL agent of SQL Server is the best example at hand for service manager, So you need to create a Service Manager using Service Controller class who may sit in the System Tray etc...

Best Regards,

RizwanSharp at 2007-8-31 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 4
I need to pass information to service like location of configuration file, CustomCommand will not be able to accomplish that becouse I can pass only integers, and I don't want to go through IPC or remoting stuff since it's so much complicates the project. Is there any other easy alternative, I really need just to tell service where configuration file lives (string value).
ChanKaiShi at 2007-8-31 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 5

Remoting is best solution for it and it'll not complicate your application. But if for some reason you dont want to use this then u can do a trick to share a Text file b/w both the application and use your Int based protocol of commuication b/w Service and ServiceController. This is not good Idea but this is the only way left. You can also use XML, Text Files, Binary Files, Registry as a communication link.

e.g Service Controls u can send 1 to Service which means ReadConfiguration File from C:\.......

int 2 Stop Services

Just like that.

Best Regards,

RizwanSharp at 2007-8-31 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 6
I'm sorry, I have to disagree that remoting is the best solution. IpcChannel is designed for just this sort of scenario and would make a lot more sense. Sharing a file is a non-starter as an idea.
GregBeech at 2007-8-31 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 7

Greg Beech wrote:
I'm sorry, I have to disagree that remoting is the best solution. IpcChannel is designed for just this sort of scenario and would make a lot more sense. Sharing a file is a non-starter as an idea.

Greg, IpcChannel is a remoting channel just like TCP or HTTP, Its not a seperate technology and it lies under Remoting if I'm not wrong.

Best Regards,

RizwanSharp at 2007-8-31 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 8

How much additional code do I have to write if I'll do remoting or IPC as compared to reading/writing from text file?

At this point while it's not elegant reading/writing from single file appears to me the best way to pass one time information from controller to service.

ChanKaiShi at 2007-8-31 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified