How to asynchronously await an Akka.Actor event?

176 views Asked by At

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?

1

There are 1 answers

2
profesor79 On

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))