Search code examples
c#.netwin32-processakka.net

How can I send a "Message" to another process in .Net, without using SendMessage?


I have multiple applications running on a single machine. One is doing work and writing logs to disk about what it has done (I'll call this WorkerApp) and another that summarizes support information about the status of WorkerApp along with some more details (I'll call this Dashboard).

From the Dashboard i want to instruct the WorkerApp to take an action (say, "Ping remote services") and I would like the WorkerApp to send the Dashboard the "pong" response when it gets it.

I have seen examples of using the SendMessage, but this seems to be pretty archaic (isn't there anything more standard now in 2016 for between process communications?).

I have very little experience with Akka.Net but the Remoting feature of it seem like a good approach, although setting this up seems a little overkill for what I would like to do.

What is the easiest way currently to go about communicating between two processes in .Net? And is there some examples of this, working on a localmachine?


Solution

  • I put together an Akka.Net example for this. This is what it looks like.

    DashBoard (sends messages)

    using System;
    using Akka.Actor;
    using Akka.Configuration;
    
    namespace DashBoard
    {
        class Program
        {
            static void Main(string[] args)
            {
                var config = ConfigurationFactory.ParseString(@"
    akka {  
        actor {
            provider = ""Akka.Remote.RemoteActorRefProvider, Akka.Remote""
        }
        remote {
            helios.tcp {
                transport-class = ""Akka.Remote.Transport.Helios.HeliosTcpTransport, Akka.Remote""
                applied-adapters = []
                transport-protocol = tcp
                port = 0
                hostname = localhost
            }
        }
    }
    ");
    
                using (var system = ActorSystem.Create("Dashboard", config))
                {
                    var server = system.ActorSelection("akka.tcp://WorkerApp@localhost:8081/user/WorkerAppActor");
                    while (true)
                    {
                        var input = Console.ReadLine();
                        server.Tell(input);
                    }
                }
            }
        }
    }
    

    WorkerApp (receives messages)

    using System;
    using Akka.Actor;
    using Akka.Configuration;
    
    namespace WorkerApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                var config = ConfigurationFactory.ParseString(@"
    akka {  
        actor {
            provider = ""Akka.Remote.RemoteActorRefProvider, Akka.Remote""
        }
        remote {
            helios.tcp {
                transport-class = ""Akka.Remote.Transport.Helios.HeliosTcpTransport, Akka.Remote""
                applied-adapters = []
                transport-protocol = tcp
                port = 8081
                hostname = localhost
            }
        }
    }
    ");
    
                using (var system = ActorSystem.Create("WorkerApp", config))
                {
                    system.ActorOf<WorkerAppActor>("WorkerAppActor");
    
                    Console.ReadLine();
                }
            }
        }
    
        class WorkerAppActor : TypedActor, IHandle<string>
        {
            public void Handle(string message)
            {
                Console.WriteLine($"{DateTime.Now}: {message}");
            }
        }
    }