Skip to content

Conversation

Arkatufus
Copy link
Contributor

Documentation for 2004 and 2005 is a repeat of 2003


uid: AK2003
title: Akka.Analyzers Rule AK2003 - "ReceiveActor.Receive message handler must not be a void async delegate."

AK2003 - Error

ReceiveActor.Receive message handler delegate must not be a void async delegate. Use ReceiveAsync instead.

Cause

ReceiveActor.Receive accepts an Action<TMessage> as a delegate, any void async delegate passed as an argument will be invoked as a detached asynchronous function that can cause erroneous message processing behavior.

An example:

using Akka.Actor;
using System.Threading.Tasks;

public sealed class MyActor : ReceiveActor
{
    public MyActor()
    {
        Receive<int>(async msg =>
            {
                await Task.Yield();
                Sender.Tell(msg);
            });
    }
}

Resolution

using Akka.Actor;
using System.Threading.Tasks;

public sealed class MyActor : ReceiveActor
{
    public MyActor()
    {
        // Use ReceiveAsync() if you're passing an asynchronous delegate
        ReceiveAsync<int>(async msg =>
            {
                await Task.Yield();
                Sender.Tell(msg);
            });
    }
}

@Aaronontheweb Aaronontheweb merged commit fd712b5 into akkadotnet:dev Jun 9, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants