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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay