Microsoft Orleans persistence providers backed by Azure Storage
$ dotnet add package Microsoft.Orleans.Persistence.AzureStorageMicrosoft Orleans Persistence for Azure Storage provides grain persistence for Microsoft Orleans using Azure Storage (Blob and Table). This allows your grains to persist their state in Azure Storage and reload it when they are reactivated.
To use this package, install it via NuGet:
dotnet add package Microsoft.Orleans.Persistence.AzureStorage
using Microsoft.Extensions.Hosting;
using Orleans.Configuration;
using Orleans.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;
// Define grain interface
public interface IMyGrain : IGrainWithStringKey
{
Task SetData(string data);
Task<string> GetData();
}
var builder = Host.CreateApplicationBuilder(args)
.UseOrleans(siloBuilder =>
{
siloBuilder
.UseLocalhostClustering()
// Configure Azure Table Storage as grain storage
.AddAzureTableGrainStorage(
name: "tableStore",
configureOptions: options =>
{
options.ConnectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
})
// Configure Azure Blob Storage as grain storage
.AddAzureBlobGrainStorage(
name: "blobStore",
configureOptions: options =>
{
options.ConnectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
});
});
var host = builder.Build();
await host.StartAsync();
// Get a reference to a grain and call it
var client = host.Services.GetRequiredService<IClusterClient>();
var grain = client.GetGrain<IMyGrain>("user123");
await grain.SetData("Hello from Azure Storage!");
var response = await grain.GetData();
// Print the result
Console.WriteLine($"Grain data: {response}");
// Keep the host running until the application is shut down
await host.WaitForShutdownAsync();
using System;
using System.Threading.Tasks;
using Orleans;
using Orleans.Runtime;
namespace ExampleGrains;
// Define grain state class
public class MyGrainState
{
public string Data { get; set; }
public int Version { get; set; }
}
// Grain implementation that uses the Azure storage
public class MyGrain : Grain, IMyGrain, IGrainWithStringKey
{
private readonly IPersistentState<MyGrainState> _state;
public MyGrain([PersistentState("state", "tableStore")] IPersistentState<MyGrainState> state)
{
_state = state;
}
public async Task SetData(string data)
{
_state.State.Data = data;
_state.State.Version++;
await _state.WriteStateAsync();
}
public Task<string> GetData()
{
return Task.FromResult(_state.State.Data);
}
}
For more comprehensive documentation, please refer to: