Search Results for

    Show / Hide Table of Contents

    AK1003 - Warning

    You should not have a synchronous code inside lambda function when using ReceiveAsync<T>() or ReceiveAnyAsync().

    Cause

    Using ReceiveAsync<T>() or ReceiveAnyAsync() with synchronous code inside their lambda function is less performant compared to Receive<T>() or ReceiveAny() because they generates an extra ActorTask message plus an additional suspend + resume of the actor's mailbox. That overhead is fine if you're await-ing something, but if the code is essentially synchronous then you should not do so.

    An example:

    using Akka.Actor;
    using System.Threading.Tasks;
    using System;
    
    public sealed class MyActor : ReceiveActor
    {
        public MyActor()
        {
            // Notice that there are no `await` inside the lambda function,
            // The lambda function is essentially synchronous
            ReceiveAsync<string>(async str => {
                Sender.Tell(str);
            }):
        }
    }
    

    Resolution

    Use Receive<T>() or ReceiveAny() instead.

    Here's an example on how to fix the above example:

    using Akka.Actor;
    using System.Threading.Tasks;
    using System;
    
    public sealed class MyActor : ReceiveActor
    {
        public MyActor()
        {
            // Notice we have replaced `ReceiveAsync` with `Receive`
            // and removed the async keyword
            Receive<string>(str => {
                Sender.Tell(str);
            }):
        }
    }
    
    In this article
    • githubEdit this page
    Back to top
    Contribute
    • Project Chat
    • Discussion Forum
    • Source Code
    Support
    • Akka.NET Support Plans
    • Akka.NET Observability Tools
    • Akka.NET Training & Consulting
    Maintained By
    • Petabridge - The Akka.NET Company
    • Learn Akka.NET