Search Results for

    Show / Hide Table of Contents

    AK1000 - Error

    Do not use the new keyword to create Akka.NET actors.

    Cause

    A class inheriting from Akka.Actor.ActorBase or one of its descendants was instantiated directly via the new keyword, which is illegal. Actors can only be instantiated inside a Props.Create method or via an IIndirectActorProducer implementation (such as Akka.DependencyInjection.)

    An example:

    using Akka.Actor;
    
    class MyActor : ActorBase {
        protected override bool Receive(object message) {
            return true;
        }
    }
    
    class Test
    {
        void Method()
        {
            MyActor actorInstance = new MyActor(); // not supported by Akka.NET
        }
    }
    

    Resolution

    The correct way to get a reference back to an actor is to wrap your actor's constructor call inside a Props.Create method and then pass that into either ActorSystem.ActorOf (when creating a top-level actor) or Context.ActorOf (when creating a child actor.)

    Here's an example below:

    using Akka.Actor;
    
    class MyActor : ReceiveActor {
        private readonly string _name;
        private readonly int _myVar;
    
        public MyActor(string name, int myVar){
            _name = name;
            _myVar = myVar;
            ReceiveAny(_ => {
                Sender.Tell(_name + _myVar);
            });
        }
    }
    
    class Test
    {
        void Method()
        {
            // obviously, don't create a new ActorSystem every time - this is just an example.
            ActorSystem sys = ActorSystem.Create("MySys");
            Akka.Actor.Props props = Akka.Actor.Props.Create(() => new MyActor("foo", 1));
            IActorRef realActorInstance = sys.ActorOf(props);
        }
    }
    
    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