Akka .NET IActorRef: Local or Remote?

Akka .NET supports location transparency. When you use an IActorRef, your application shouldn’t care whether that actor is running on the same machine or somewhere else on the network. You can change where an actor runs as a matter of configuration, and your application will never know the difference.

Although an application shouldn’t depend on the physical location of an actor to perform its logic, knowing where an actor is running can be useful (e.g. when troubleshooting issues).

akkanet-islocal

There is an IsLocal property that you can use to tell whether an actor is running locally or remotely. However, this is not immediately accessible from the IActorRef. Instead, you need to cast your IActorRef to an InternalActorRefBase to be able to use it:

(localChatActor as InternalActorRefBase).IsLocal

If you’re working with an ActorSelection (which you probably are if you’re using remote actors), then you will first want to get to the particular IActorRef of the actor. You can do this via the ActorSelection‘s Anchor property.

(remoteChatActor.Anchor as InternalActorRefBase).IsLocal

This will allow you to check whether an actor is running locally or remotely. But remember: use this only for diagnostic purposes, and don’t make your application code dependent on it.