The Actor framework doesn't differentiate much between creating an Actor the first time and activating an Actor that has been deactivated and lies dormant. Both actions will create a new instance of the Actor implementation (calls .ctor) and then calls OnActivateAsync
. It does this before it executes any method dispatched to the Actor. There is no means for you to create an instance of an Actor without calling a method on the Actor's interface, it is all part of the underlying ActorBase
and ActorManager
's handling of messages.
Also, slighlty depending on the type of persistence you have chosen for your actor (None
, Volatile
or Persisted
), the ActorService hosting the Actor stores a persistence key with the IActorStateProvider
associated with the Actor/ActorService. This is how the ActorService "knows" about it's actors as well. When you ask an ActorService about known Actors, it queries it's StateProvider for
If you want to run some Initialization code the first time, and only the first time an Actor is activated then you could add a state key for that Actor in OnActivateAsync
:
protected override async Task OnActivateAsync()
{
ActorEventSource.Current.ActorMessage(this, "Actor activated.");
var initialized = await this.StateManager.ContainsStateAsync("initalized");
if (!initialized) await Initialize();
}
private async Task Initialize()
{
ActorEventSource.Current.ActorMessage(this, "Actor initialized.");
await this.StateManager.AddStateAsync("initialized", true);
}