Skip to content

Instantly share code, notes, and snippets.

@skotzko
Last active July 29, 2020 04:44
Show Gist options
  • Save skotzko/de23e02aa71a6817aa09 to your computer and use it in GitHub Desktop.
Save skotzko/de23e02aa71a6817aa09 to your computer and use it in GitHub Desktop.
Akka.NET Fundamentals Cheatsheet

Akka.NET Fundamentals Cheatsheet

Workshop materials

  1. Download starter solution file here
  2. Download Workbook here

Actor Fundamentals

Only 3 fundamental properties of an actor:

  1. Send messages
  2. Create others actors
  3. Switch behavior

Actors also have globally unique addrses within their ActorSystem.

Read "What is An Actor?" here.

Creating actors

IActorRefs

IActorRefs are a handle to an actor. You send messages to it, and the ActorSystem is responsible for delivering them (often over the network / between processes).

Make Props

var props = Props.Create(() => new MyActorClass(actorDependency));

Creating actors & ActorSystems

MyActorSystem = ActorSystem.Create("MyActorSystem");
var dependency = "some string my actor depends on";
var myActor = MyActorSystem.ActorOf(Props.Create(() => new MyActorClass(dependency)), "MyActor");

Child actors

// create child
Context.ActorOf(Props.Create(() => new MyChildActorClass()), "childActorName");

// look up child actor
Context.Child("childActorName");

Messages

Messages are just POCOs. Nothing scary here.

Sending messages

// just Tell the message to the actor
myActor.Tell(new StartProcessing());

Hierarchies & Supervision

All actors have a parent (who supervises them), and all actors can be a parent.

Learn more about hierarchies here.

ActorSelection

ActorSelection is the practice of looking up an actor by its ActorPath.

Generally, you want to favor IActorRefs over ActorSelections. Learn more here.

Actor Lifecycle

All actors have a lifecycle, and you can tap into it.

Stages of the actor lifecycle

  1. Starting
  2. Receiving
  3. Stopping
  4. Terminated
  5. Restarting

Lifecycle hook methods

  1. PreStart
  2. PostStop
  3. PreRestart
  4. PostRestart

Akka.NET Actor Lifecycle Stages & Hook Methods

Learn more here.

@PalashBansal
Copy link

Nice cheatsheet. I guess a little about switchable behavior can be written like Become() method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment