J M Smith
J M Smith

Reputation: 35

Akka.Net Invoke an Action inside an Actor class from a non-Actor class

Title pretty much says it all. I want to invoke an Action in an Actor class from a non-Actor class that is not in the same folder as the Actor if that is possible.

Upvotes: 0

Views: 172

Answers (1)

Sadral
Sadral

Reputation: 46

In the actor model, you don't "invoke an action" on the actor. You can ask for something or tell it something.
To do that, you need the actor's address, in akka.net you need an IActorRef.

I'm not sure what you mean by "folder", I'll just enumerate the possible meanings and the answer to each :

  1. Same physical folder
    • The physical location of the actor class and the physical location of the "calling class" are not related to the ability to send messages to an actor.
  2. Same namespace folder
    • The namespace does not directly influence the ability to send a message to an actor. It may only be influenced by the visibility of the message class to send.
  3. Same address folder
    • Again, the address folder does not influence the ability to send a message to the actor.

To be able to send a message to an actor you only need it's IActorRef, there are multiple ways to manage those references that I'm not going write here because it depends on your application architecture.

I will, however, mention that you can also use the ActorSystem.ActorSelection method to obtain the reference of an actor that's located at a certain path, but this is not recommended because it may easily be misused and can lead to some bad habits. It's usage should be considered an advanced feature.

Once you got your hand on the actor reference, you then can call the Ask method from your non-actor class if you expect some result (beware, the result will be wrapped in a Task that you'll have to manage to retrieve the result).
If you don't expect an answer (or if you don't care about the response), you can just call the Tell method.
You have to pass the message to send to the actor to both of these methods.

Finally, once the actor receives your message, your message will eventually be processed and the actor will then do whatever it was designed to do.

If you haven't done it yet, the bootcamp is a good way to learn the basis of akka.net.

Upvotes: 1

Related Questions