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?
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);
}