Skip to content

Commit a91adbf

Browse files
authored
[Docs] Add AK1008 documentation (#7504)
1 parent 50b6cfe commit a91adbf

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
uid: AK1008
3+
title: Akka.Analyzers Rule AK1008 - "Creating actors using `ActorSystem.ActorOf()` inside an actor"
4+
---
5+
6+
# AK1008 - Warning
7+
8+
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.
9+
10+
## Cause
11+
12+
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.
13+
14+
You can suppress this warning message if you specifically intended to create a new actor under the "/user" guardian actor.
15+
16+
Example:
17+
18+
```csharp
19+
using System;
20+
using Akka.Actor;
21+
22+
public sealed class FirstActor : ReceiveActor
23+
{
24+
public FirstActor()
25+
{
26+
Context.System.ActorOf<MyActor>("newActor");
27+
}
28+
}
29+
30+
public sealed class MyActor : ReceiveActor
31+
{
32+
}
33+
```
34+
35+
![Actor created in wrong path](/images/debugging/rules/AK1008-Wrong-path.png)
36+
37+
## Resolution
38+
39+
Use `Context.ActorOf()` or `Context.ActorOf<T>()` method instead to create a new child actor:
40+
41+
```csharp
42+
using System;
43+
using Akka.Actor;
44+
45+
public sealed class FirstActor : ReceiveActor
46+
{
47+
public FirstActor()
48+
{
49+
Context.ActorOf<MyActor>("newActor");
50+
}
51+
}
52+
53+
public sealed class MyActor : ReceiveActor
54+
{
55+
}
56+
```
57+
58+
![Actor created as child actor](/images/debugging/rules/AK1008-Right-path.png)

docs/articles/debugging/rules/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
href: AK1006.md
1515
- name: AK1007
1616
href: AK1007.md
17+
- name: AK1008
18+
href: AK1008.md
1719
- name: AK2000
1820
href: AK2000.md
1921
- name: AK2001
29 KB
Loading
28 KB
Loading

0 commit comments

Comments
 (0)