Skip to content

Commit

Permalink
[Docs] Add AK1008 documentation (#7504)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkatufus authored Feb 14, 2025
1 parent 50b6cfe commit a91adbf
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
58 changes: 58 additions & 0 deletions docs/articles/debugging/rules/AK1008.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
uid: AK1008
title: Akka.Analyzers Rule AK1008 - "Creating actors using `ActorSystem.ActorOf()` inside an actor"
---

# AK1008 - Warning

Creating actors using `Context.System.ActorOf()` inside an actor is discouraged because the resulting actor would not be the child of this actor but the "/user" guardian actor itself. Please use `Context.ActorOf()` if your intention is to create a child actor of this actor.

## Cause

Creating an `IActorRef` Using `Context.System.ActorOf()` or `Context.System.ActorOf<T>()` inside a class that inherits `ReceiveActor` or `UntypedActor` would create an actor as a child the "/user" guardian actor instead of a child actor under the invoking actor.

You can suppress this warning message if you specifically intended to create a new actor under the "/user" guardian actor.

Example:

```csharp
using System;
using Akka.Actor;

public sealed class FirstActor : ReceiveActor
{
public FirstActor()
{
Context.System.ActorOf<MyActor>("newActor");
}
}

public sealed class MyActor : ReceiveActor
{
}
```

![Actor created in wrong path](/images/debugging/rules/AK1008-Wrong-path.png)

## Resolution

Use `Context.ActorOf()` or `Context.ActorOf<T>()` method instead to create a new child actor:

```csharp
using System;
using Akka.Actor;

public sealed class FirstActor : ReceiveActor
{
public FirstActor()
{
Context.ActorOf<MyActor>("newActor");
}
}

public sealed class MyActor : ReceiveActor
{
}
```

![Actor created as child actor](/images/debugging/rules/AK1008-Right-path.png)
2 changes: 2 additions & 0 deletions docs/articles/debugging/rules/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
href: AK1006.md
- name: AK1007
href: AK1007.md
- name: AK1008
href: AK1008.md
- name: AK2000
href: AK2000.md
- name: AK2001
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a91adbf

Please # to comment.