AK2001 - Warning
Do not use automatically handled messages in inside Akka.Cluster.Sharding.IMessageExtractor
s.
Cause
As of Akka.NET v1.5.15, Akka.Cluster.Sharding is guaranteed to automatically handle the following built-in messages:
ShardRegion.StartEntity
- used whenever Akka.Cluster.Sharding'sremember-entities
feature is enabled.ShardingEnvelope
- a generic envelope type that can be used to send arbitrary messages to entity actors.
Whenever a user tries to manually handle either of these messages, they're performing duplicate work - this rule is in effect to spot wasteful situations and to automatically provide a Roslyn code fix to resolve them.
An example:
using Akka.Cluster.Sharding;
public sealed class MessageExtractor : HashCodeMessageExtractor
{
public MessageExtractor() : base(maxNumberOfShards: 100) { }
public string EntityId(object message)
{
return message switch
{
string sharded => sharded,
ShardingEnvelope e => e.EntityId,
ShardRegion.StartEntity start => start.EntityId,
_ => null,
};
}
}
Resolution
Akka.Analyzers comes with a code fix for this issue, which if accepted by the end user, will rewrite the previous example to the following:
using Akka.Cluster.Sharding;
public sealed class MessageExtractor : HashCodeMessageExtractor
{
public MessageExtractor() : base(maxNumberOfShards: 100) { }
public string EntityId(object message)
{
return message switch
{
string sharded => sharded,
_ => null,
};
}
}