1

I am using akka.NET. In most cases we use akka like this:

class ActorA : UntypedActor
{
    public delegate void EventHandler(object arg1, object arg2, ...);
    public static event EventHandler Event;
}
actorA.Event += some_function;

In this case we execute some_function(arg1, arg2) whenever Event.Invoke(arg1, arg2) is called. Now assume that we have an asynchrounous HTTP server, and I am trying to let the server asynchronously await actorA.Event to happen, after a client calls the server. I do not need to run some_function when Event happens, but I have to ensure that the runtime context is switched back into the functions of the HTTP server. That is:

// in the methods of the HTTP server...
public async void AwaitAnEvent()
{
    await ReturnOnEvent(actorA.Event);
}

Is it possible to efficiently implement ReturnOnEvent which returns immediately when the next actorA.Event.Invoke(arg1, arg2) is called?

2474101468
  • 328
  • 2
  • 9
  • Akka's actors shall communicate with other actors through messages. For other cases, I would recommend reading this article. https://petabridge.com/blog/async-await-vs-pipeto/ – martikyan Dec 04 '22 at 20:23
  • I'm not sure I fully understand your question - is the `EventHandler` being called from outside the actor? Why not just send a message to the actor via `IActorRef.Tell`? – Aaronontheweb Dec 05 '22 at 19:17
  • @Aaronontheweb I have edited the question supplying more details. `Tell` seems to provide no way for me to switch to the context of another method – 2474101468 Dec 08 '22 at 07:40

1 Answers1

0

This case smells a bit. inside the web controller just ask your actor and that will trigger it to generate response.

When asking it will await in the controller to get response.

var response =  myActor.Ask<ResponseType>(new GiveMySomeFoodMsg());
-- in the actor
Sender.Tell(new ResponseType(some data here))
profesor79
  • 9,213
  • 3
  • 31
  • 52