// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ------------------------------------------------------------
namespace DaprDemoActor
{
using System;
using System.Threading.Tasks;
using Dapr.Actors;
using Dapr.Actors.Runtime;
using IDemoActorInterface;
///
/// Actor Implementation.
/// Following example shows how to use Actor Reminders as well.
/// For Actors to use Reminders, it must derive from IRemindable.
/// If you don't intend to use Reminder feature, you can skip implementing IRemindable and reminder specific methods which are shown in the code below.
///
public class DemoActor : Actor, IDemoActor, IRemindable
{
private const string StateName = "my_data";
///
/// Initializes a new instance of the class.
///
/// Actor Service hosting the actor.
/// Actor Id.
public DemoActor(ActorService service, ActorId actorId)
: base(service, actorId)
{
}
///
public async Task SaveData(MyData data)
{
Console.WriteLine($"This is Actor id {this.Id} with data {data}.");
// Set State using StateManager, state is saved after the method execution.
await this.StateManager.SetStateAsync(StateName, data);
}
///
public Task GetData()
{
// Get state using StateManager.
return this.StateManager.GetStateAsync(StateName);
}
///
public Task TestThrowException()
{
throw new NotImplementedException();
}
///
public Task TestNoArgumentNoReturnType()
{
return Task.CompletedTask;
}
///
public async Task RegisterReminder()
{
await this.RegisterReminderAsync("TestReminder", null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5));
}
///
public Task UnregisterReminder()
{
return this.UnregisterReminderAsync("TestReminder");
}
///
public Task ReceiveReminderAsync(string reminderName, byte[] state, TimeSpan dueTime, TimeSpan period)
{
// This method is invoked when an actor reminder is fired.
var actorState = this.StateManager.GetStateAsync(StateName).GetAwaiter().GetResult();
actorState.PropertyB = $"Reminder triggered at '{DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss")}'";
this.StateManager.SetStateAsync(StateName, actorState);
return Task.CompletedTask;
}
///
public Task RegisterTimer()
{
return this.RegisterTimerAsync("TestTimer", this.TimerCallBack, null, TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(3));
}
///
public Task UnregisterTimer()
{
return this.UnregisterTimerAsync("TestTimer");
}
///
/// This method is called whenever an actor is activated.
/// An actor is activated the first time any of its methods are invoked.
///
/// A task that represents the asynchronous operation.
protected override Task OnActivateAsync()
{
// Provides opportunity to perform some optional setup.
return Task.CompletedTask;
}
///
/// This method is called whenever an actor is deactivated after a period of inactivity.
///
/// A task that represents the asynchronous operation.
protected override Task OnDeactivateAsync()
{
// Provides Opportunity to perform optional cleanup.
return Task.CompletedTask;
}
///
/// This method is called when the timer is triggered based on its registration.
/// It updates the PropertyA value.
///
/// Timer input data.
/// A task that represents the asynchronous operation.
private Task TimerCallBack(object data)
{
var state = this.StateManager.GetStateAsync(StateName).GetAwaiter().GetResult();
state.PropertyA = $"Timer triggered at '{DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss")}'";
this.StateManager.SetStateAsync(StateName, state);
return Task.CompletedTask;
}
}
}