DEV Community

Cover image for Hello World with Proto.Actor: Building Your First Actor Model System in .NET
Rafael Andrade
Rafael Andrade

Posted on

Hello World with Proto.Actor: Building Your First Actor Model System in .NET

In my previous article about the Actor Model, I introduced the Proto.Actor framework. Today, I’ll walk through a simple "Hello World" example to demonstrate its core concepts.

Why Proto.Actor?

Proto.Actor was created by the developer behind the initial port of Akka (from Java to .NET). Building on lessons learned during that project, the team prioritized leveraging existing technologies over reinventing the wheel. For example, Proto.Actor uses gRPC for serialization instead of maintaining a custom solution like Akka.NET. This focus on simplicity and interoperability makes it a compelling choice for actor-based systems.

Another standout feature is Proto.Actor’s excellent documentation, which provides deep insights into both the Actor Model and practical framework usage.

Prerequisites

  • .NET 6+ SDK installed
  • Basic familiarity with C#

Step 1: Create a Project

  1. Create a new console project:
dotnet new console -n ProtoActorDemo  
Enter fullscreen mode Exit fullscreen mode
  1. Add the Proto.Actor package:
dotnet add package Proto.Actor  
Enter fullscreen mode Exit fullscreen mode

Step 2: Define Messages

Actors communicate via messages. In C#, record types are ideal due it immutability

public record Hello(string Who);  
Enter fullscreen mode Exit fullscreen mode

Why immutability? Immutable messages prevent side effects and ensure thread safety—a core tenet of the Actor Model.

Step 3: Implement an Actor

Actors process messages asynchronously. Create a GreetingActor by implementing IActor:

public class GreetingActor : IActor  
{
    public Task ReceiveAsync(IContext context)
    {
        if (context.Message is Hello hello)
        {
            Console.WriteLine($"Hello {hello.Who}!");
        }
        return Task.CompletedTask;
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Start the Actor System

Initialize the system, spawn the actor, and send messages:

var system = new ActorSystem();  
var props = Props.FromProducer(() => new GreetingActor());
var greeter = system.Root.Spawn(props);  

while (true)  
{  
    Console.Write("Enter your name (q to quit): ");  
    var name = Console.ReadLine();  

    if (string.IsNullOrEmpty(name))
    { 
        continue;  
    }

    if (name == "q") 
    {
        break;  
    }

    system.Root.Send(greeter, new Hello(name)); 
    await Task.Delay(TimeSpan.FromSeconds(1)); // Give some time to Actor system process the message 
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

In under 20 lines of code, we’ve built a functional actor system! Proto.Actor’s simplicity and documentation make it easy to get started.

Next up: We’ll explore virtual actors (a.k.a. "grains").

Reference

Proto.Actor Official Documentation

Complete Code Sample

Top comments (1)

Collapse
 
sofacts profile image
sofactos21

Proto.Actor’s innovative approach mirrors how celebrities optimize strategies to grow their net worth. For more on tech trends and financial insights, visit sofacts.com for fast, engaging updates.