4

Is there a way to communicate between two different services? I have a service that already runs. Is there a way to create a second service that can attach to the first service and send and receive dates to it?

I would also like to access the Windows service from a console application and attach to it. Is it possible?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
elisa
  • 743
  • 2
  • 13
  • 31
  • Can you tell us more? Are you able to modify the running service's code? If not, what protocol(s) does it support/use? – Emond Mar 30 '11 at 13:28
  • the first service is running an exe file, for example notepad.exe. From service2 i would like to write datas in notepad.exe though service1. – elisa Mar 30 '11 at 13:30
  • Windows Services can't run EXE files like "notepad.exe". This doesn't make any sense. Why don't you just save to a log file from a single service? – Cody Gray - on strike Mar 30 '11 at 13:45
  • @Cody Gray: acutally windows service can run files like "notepad.exe". You can set service permission to allow to interact with the desktop. – HABJAN Mar 30 '11 at 13:48
  • 1
    @HABJAN: Only if you're still running Windows XP. That was 10 years ago. Windows Vista and later have broken the ability to interact with the desktop for security reasons. You weren't supposed to use that for anything other than testing in earlier versions of Windows, but developers didn't listen, and customers complained about malware and Windows' general lack of security. – Cody Gray - on strike Mar 30 '11 at 13:48
  • @Cody Gray: You can write WCF service and host it in Windows Service and from WCF service you can run EXE. That works in Vista and Win 7. – HABJAN Mar 30 '11 at 13:55
  • What do oyou mean by WCF services? Can I run a .exe file using wcf service and than if i put it in a service..will it run in background? – elisa Mar 30 '11 at 13:56
  • @elisa: what i wrote was for @Cody Gray :-) i did not want to confuze you. Here is example of what i was telling about to @Cody Gray: http://www.codeproject.com/KB/cs/ServiceDesktopInteraction.aspx – HABJAN Mar 30 '11 at 13:58

2 Answers2

7

You can try to implement this by using:

Example of using WCF: Many to One Local IPC using WCF and NetNamedPipeBindin.

Other example: A C# Framework for Interprocess Synchronization and Communication.

Everything depends on what version of .NET Framework you use. If you use .NET 3.0 and above then you can take a look into WCF. If not then you are on your own and you can google on keywords P/Invoke (CreateFileMapping, MapViewOfFile, CreatePipe...).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HABJAN
  • 9,212
  • 3
  • 35
  • 59
  • using sockets? How can i do that/ Which is the easiest solution? If one of my app is a console app can i make a link to a windows service an communicate with it? – elisa Mar 30 '11 at 13:35
  • @elisa: By using sockets i was thinking on standard client-server TCP/IP communication, only thing is that everything will work on localhost. Take a look at: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx, there you have small example at the end of the page. If you ask me i would go with WCF/NamedPipes. – HABJAN Mar 30 '11 at 13:41
  • In .NET 2.0 you can use .NET Remoting. I would prefer it over mentioned methods if I couldn't use WCF. – Dmitrii Lobanov Mar 30 '11 at 13:43
  • @Dmitry Lobanov: yep, .net remoting with full-duplex communication can be used. – HABJAN Mar 30 '11 at 13:45
  • can i connect to s service from a console app? is it possible? can you give me a very short and easy example? – elisa Mar 30 '11 at 13:46
  • ps Dmitry what do you mean by .net remoting? – elisa Mar 30 '11 at 13:46
  • 2elisa: Here http://msdn.microsoft.com/en-us/library/kwdt6w2k%28v=VS.80%29.aspx you can read a lot about .NET Remoting. – Dmitrii Lobanov Mar 30 '11 at 13:47
  • @elisa: you can connect from any service or win/console app to any service or win/console app. Service listens on specific TCP port, console app connects to a prot that serive listens. Everything else is pure TCP/IP communication. Remoting in this case would be simpler as you dont need to worry about transmitted data format. – HABJAN Mar 30 '11 at 13:51
  • thx. can you make a really short eg where you have a service and a concole app that sends data to it? thx:) – elisa Mar 30 '11 at 13:54
  • @elisa here is a very simple example on how to use .NET Remoting: http://generally.wordpress.com/2007/05/31/a-simple-remoting-example-in-c/ – ntziolis Mar 30 '11 at 13:59
  • @elisa: short eg.? Can you be more specific? – HABJAN Mar 30 '11 at 13:59
  • thx ntziolis. So the server part will be addet in the windows service and the client part in the console app? – elisa Mar 30 '11 at 14:06
  • @elisa: if you will have 1 or more console apps and only 1 service then yes server part will be added to service. – HABJAN Mar 30 '11 at 14:09
  • 2
    Do not use .NET Remoting. All of the MSDN documentation says that .NET Remoting is a legacy technology that is maintained for backward compatibility. All new distributed development should use WCF. – Matt Davis Mar 30 '11 at 14:32
  • I agree with Matt. I can't find any mention (in either the question or the comments) that elisa is limited to .NET 2.0. In that case, you should definitely prefer WCF, as .NET Remoting has been roundly deprecated and should only be used where WCF is not available. – Cody Gray - on strike Mar 31 '11 at 05:17
3

To begin with I would play around with tcpclient and tcpserver

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx

Even if the data you need to send is more complex than a date it can easily be serialized/deserialized.

For sending and receiving dates this seams the simplest option.

Also socks work if the services run on different machines whereas shared memory and namedpipes don't.

example code

// Create a thread running this code in your onstarted method of the service

using System.IO;
using System.Net;
using System.Net.Sockets;

var server = new TcpListener(IPAddress.Parse("127.0.0.1"), 8889);
server.Start();

while(true) {
  var client = server.AcceptTcpClient(); 

  using(var sr = new StreamReader(client.GetStream())) {
    var date = DateTime.Parse(sr.ReadToEnd());
    Console.WriteLine(date);
  } 
}

// In the console

using System.IO;
using System.Net;
using System.Net.Sockets;

var client = new TcpClient("localhost",8889); 
using(var sw = new StreamWriter(client.GetStream())) {
  sw.Write(System.DateTime.Now);
}
VansFannel
  • 45,055
  • 107
  • 359
  • 626
James Kyburz
  • 13,775
  • 1
  • 32
  • 33
  • both services run on the same machine. The first service is node using regedit. I just want to create a second service and acces the first one , access it's application and send data. could you give me a short eg of how can i do it? thx – elisa Mar 30 '11 at 13:43
  • or..if i run a console app how could i connect to a service? – elisa Mar 30 '11 at 13:44
  • I can't say using sockets on local system is a good IPC mechanism, especially for beginner. – Eugene Mayevski 'Callback Mar 30 '11 at 15:37
  • Sure, neither is creating a windows service. The classes I show are high level abstractions – James Kyburz Mar 30 '11 at 16:09
  • hi.thx a lot. if i have an exe file running on my machine can i make the tcp ip connection between them based on your ex? – elisa Mar 31 '11 at 13:04