Azure-Samples/service-fabric-dotnet-getting-started

Actor service does not function correctly

Dismissile opened this issue · 1 comments

This is the code for the Actor service:

public async Task StartProcessingAsync(CancellationToken cancellationToken)
{
    try
    {
        // this throws an exception
        this.GetReminder(ReminderName);
            
        // therefore this never gets called
        bool added = await this.StateManager.TryAddStateAsync<long>(StateName, 0);

        if (!added)
        {
            // value already exists, which means processing has already started.
            throw new InvalidOperationException("Processing for this actor has already started.");
        }
    }
    catch (ReminderNotFoundException)
    {
        await this.RegisterReminderAsync(ReminderName, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(10));
    }
}

public async Task ReceiveReminderAsync(string reminderName, byte[] context, TimeSpan dueTime, TimeSpan period)
{
    if (reminderName.Equals(ReminderName, StringComparison.OrdinalIgnoreCase))
    {
        // since TrySetState never gets called -- this will cause an exception
        long currentValue = await this.StateManager.GetStateAsync<long>(StateName);

        ActorEventSource.Current.ActorMessage(this, $"Processing. Current value: {currentValue}");

        await this.StateManager.SetStateAsync<long>(StateName, ++currentValue);
    }
}

The ActorBackendService controller will use the proxy to call this method. GetReminder will throw an exception which will go to the catch block and call RegisterReminderAsync.

Once the reminder is received it tries to use GetState but the state was never initialized because GetReminder threw an exception and skipped the rest of the code.

Fixed...