![]() |
VOOZH | about |
dotnet add package Hexalith.KeyValueStorages.DaprComponents --version 2.1.2
NuGet\Install-Package Hexalith.KeyValueStorages.DaprComponents -Version 2.1.2
<PackageReference Include="Hexalith.KeyValueStorages.DaprComponents" Version="2.1.2" />
<PackageVersion Include="Hexalith.KeyValueStorages.DaprComponents" Version="2.1.2" />Directory.Packages.props
<PackageReference Include="Hexalith.KeyValueStorages.DaprComponents" />Project file
paket add Hexalith.KeyValueStorages.DaprComponents --version 2.1.2
#r "nuget: Hexalith.KeyValueStorages.DaprComponents, 2.1.2"
#:package Hexalith.KeyValueStorages.DaprComponents@2.1.2
#addin nuget:?package=Hexalith.KeyValueStorages.DaprComponents&version=2.1.2Install as a Cake Addin
#tool nuget:?package=Hexalith.KeyValueStorages.DaprComponents&version=2.1.2Install as a Cake Tool
This library provides a Dapr actor-based implementation of the key-value storage interface. It uses Dapr actors to ensure concurrency control and data consistency.
Add the package to your project:
dotnet add package Hexalith.KeyValueStorages.DaprComponents
using Hexalith.KeyValueStorages;
using Hexalith.KeyValueStorages.DaprComponents.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Register the Dapr actor key-value storage
builder.Services.AddDaprActorKeyValueStorage<string, MyData, KeyToStringSerializer<string>, MyDataSerializer>();
var app = builder.Build();
// Configure the Dapr actors middleware
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapActorsHandlers();
});
app.Run();
using System.Text.Json;
using Hexalith.KeyValueStorages;
public class MyDataSerializer : IValueSerializer<MyData, string>
{
public string DataType => "application/json";
public (MyData Value, string Etag) Deserialize(string value)
{
var document = JsonDocument.Parse(value);
var root = document.RootElement;
string etag = root.GetProperty("etag").GetString() ?? throw new InvalidOperationException("Etag is missing");
MyData data = JsonSerializer.Deserialize<MyData>(root.GetProperty("data").GetRawText())
?? throw new InvalidOperationException("Failed to deserialize data");
return (data, etag);
}
public async Task<(MyData Value, string Etag)> DeserializeAsync(Stream stream, CancellationToken cancellationToken)
{
using var document = await JsonDocument.ParseAsync(stream, cancellationToken: cancellationToken);
var root = document.RootElement;
string etag = root.GetProperty("etag").GetString() ?? throw new InvalidOperationException("Etag is missing");
MyData data = JsonSerializer.Deserialize<MyData>(root.GetProperty("data").GetRawText())
?? throw new InvalidOperationException("Failed to deserialize data");
return (data, etag);
}
public string Serialize(MyData value, string etag)
{
var wrapper = new { data = value, etag };
return JsonSerializer.Serialize(wrapper);
}
public async Task SerializeAsync(Stream stream, MyData value, string etag, CancellationToken cancellationToken)
{
var wrapper = new { data = value, etag };
await JsonSerializer.SerializeAsync(stream, wrapper, cancellationToken: cancellationToken);
}
}
public class MyService
{
private readonly IKeyValueStore<string, MyData, string> _storage;
public MyService(IKeyValueStore<string, MyData, string> storage)
{
_storage = storage;
}
public async Task SaveDataAsync(string key, MyData data, CancellationToken cancellationToken)
{
// Add new data
string etag = await _storage.AddAsync(key, data, cancellationToken);
// Update existing data
data.UpdatedAt = DateTime.UtcNow;
string newEtag = await _storage.SetAsync(key, data, etag, cancellationToken);
// Get data
StoreResult<MyData, string> result = await _storage.GetAsync(key, cancellationToken);
Console.WriteLine($"Data: {result.Value}, Etag: {result.Etag}");
}
}
The Dapr actor-based key-value storage uses Dapr actors to ensure that only one operation can be performed on a key at a time. This prevents race conditions and ensures data consistency.
You can configure the actor settings when registering the key-value storage:
builder.Services.AddDaprActorKeyValueStorage<string, MyData, KeyToStringSerializer<string>, MyDataSerializer>(
actorTypeName: "CustomKeyValueStoreActor");
You can specify a custom actor type name when registering the key-value storage:
builder.Services.AddDaprActorKeyValueStorage<string, MyData, KeyToStringSerializer<string>, MyDataSerializer>(
actorTypeName: "CustomKeyValueStoreActor");
You can create a custom key serializer by implementing the IKeySerializer<TKey> interface:
public class CustomKeySerializer<TKey> : IKeySerializer<TKey>
where TKey : notnull
{
public string Serialize(TKey key)
{
// Custom serialization logic
return key.ToString() ?? throw new InvalidOperationException("Key cannot be null");
}
}
You can create a custom value serializer by implementing the IValueSerializer<TValue, TEtag> interface:
public class CustomValueSerializer<TValue> : IValueSerializer<TValue, string>
{
public string DataType => "application/custom";
public (TValue Value, string Etag) Deserialize(string value)
{
// Custom deserialization logic
// ...
}
public Task<(TValue Value, string Etag)> DeserializeAsync(Stream stream, CancellationToken cancellationToken)
{
// Custom async deserialization logic
// ...
}
public string Serialize(TValue value, string etag)
{
// Custom serialization logic
// ...
}
public Task SerializeAsync(Stream stream, TValue value, string etag, CancellationToken cancellationToken)
{
// Custom async serialization logic
// ...
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.1.2 | 186 | 4/12/2025 |
| 2.1.1 | 228 | 4/11/2025 |
| 2.1.0 | 223 | 4/11/2025 |
| 2.0.2 | 243 | 4/10/2025 |
| 2.0.1 | 229 | 4/10/2025 |
| 2.0.0 | 253 | 4/10/2025 |
| 1.6.0 | 226 | 4/9/2025 |
| 1.5.0 | 244 | 4/8/2025 |
| 1.4.2 | 239 | 4/8/2025 |
| 1.4.1 | 235 | 4/7/2025 |
| 1.4.0 | 253 | 4/7/2025 |
| 1.3.2 | 220 | 4/6/2025 |
| 1.3.1 | 233 | 4/6/2025 |
| 1.3.0 | 252 | 4/6/2025 |
| 1.2.0 | 235 | 4/6/2025 |