No, you cannot use async / await or any variant of it inside the actor’s mailbox and get safe results.
Each actor maintains its own context, which includes important details, such as the sender of the previous message and other important states that may change. The actors process the messages one at a time, therefore, as soon as the call inside his mailbox ends, he immediately starts processing his next message - if you place the waiting call inside the mailbox, the actor will process a completely different message than the one you started with while waiting your expectation.
The best example of using asynchronous calls and TAP inside actors is to use the PipeTo template. It looks like we don't have documentation for this yet at http://akkadotnet.imtqy.com/ , so I will give you a sample code in the real world (in C #):
public void Handle(ExplicitReplyOperation<CampaignsForAppRequest> message) { Context.IncrementMessagesReceived(); _loaderActor.Ask(message.Data).ContinueWith(r => { var campaigns = (IList<Campaign>)r.Result; message.Originator.Tell(new CampaignsForAppResponse() { AppId = message.Data.AppId, ActiveCampaigns = campaigns }, ActorRef.NoSender); return campaigns; }).PipeTo(Self); }
In this example, I have a TypedActor that continues the job, does some post-processing, and then uses the PipeTo operator (the Akka.NET extension method that you can apply to any Task object) to connect the results of the task to this actor’s mailbox after completion operations. That way, I can close any state I need, and my actor can continue to process messages securely while this asynchronization operation continues.
Aaronontheweb
source share