MaksIT.Dapr 2.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MaksIT.Dapr --version 2.1.0
                    
NuGet\Install-Package MaksIT.Dapr -Version 2.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MaksIT.Dapr" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MaksIT.Dapr" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="MaksIT.Dapr" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MaksIT.Dapr --version 2.1.0
                    
#r "nuget: MaksIT.Dapr, 2.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MaksIT.Dapr@2.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MaksIT.Dapr&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=MaksIT.Dapr&version=2.1.0
                    
Install as a Cake Tool

MaksIT.Dapr

Line Coverage Branch Coverage Method Coverage

This repository hosts the maksit-dapr project, which utilizes Dapr (Distributed Application Runtime) to facilitate building and managing microservices with ease. The project focuses on implementing a robust, scalable solution leveraging Dapr's building blocks and abstractions.

Table of Contents

Overview

maksit-dapr serves as a foundational project to explore and implement Dapr-based microservices, demonstrating the integration of Dapr�s pub-sub, bindings, state management, and other building blocks in a distributed system environment.

Features

  • Pub-Sub Integration: Uses Dapr's pub-sub component for seamless event-driven communication.
  • State Management: Efficient, distributed state handling across microservices.

Getting Started

Ensure that you have the following installed:

Installation

To install MaksIT.Core, add the package to your project via NuGet:

dotnet add package MaksIT.Dapr

Or manually add it to your .csproj file:

<PackageReference Include="MaksIT.Dapr" Version="1.0.0" />

Usage

Registering Dapr Services

To use maksit-dapr in your application, you must register the provided services for dependency injection. Follow these steps to integrate Dapr's pub-sub and state management capabilities in your ASP.NET Core application:

  1. Register the Publisher and State Store Services: Add these services in Program.cs or Startup.cs.
using MaksIT.Dapr.Extensions;

var builder = WebApplication.CreateBuilder(args);

// Register Dapr services
builder.Services.RegisterPublisher();
builder.Services.RegisterStateStore();
// HA coordination (leases via Dapr state Component — e.g. persistent NATS KV)
builder.Services.RegisterWorkLeases();

var app = builder.Build();

// Set up Dapr subscriber middleware (optional)
// Only after Webapi Authorization services
app.RegisterSubscriber();
  1. Use Controller as a Dapr Subscriber:

To designate a controller as a Dapr subscriber, annotate it with the [Topic("pubsubName", "name")] attribute:

using Dapr;

[Topic("my-pubsub", "my-topic")]
public class MyController : ControllerBase
{
    [HttpPost("/my-endpoint")]
    public IActionResult ReceiveMessage([FromBody] MyCommand payload)
    {


        // Handle message
        return Ok();
    }
}

Injecting and Using Dapr Services

With the services registered, you can inject IDaprPublisherService and IDaprStateStoreService into controllers or other services as needed:

using MaksIT.Dapr;

public class MyService
{
    private readonly IDaprPublisherService _publisher;
    private readonly IDaprStateStoreService _stateStore;

    public MyService(IDaprPublisherService publisher, IDaprStateStoreService stateStore)
    {
        _publisher = publisher;
        _stateStore = stateStore;
    }

    public async Task PublishEventAsync()
    {
        var command = new MyCommand
        {
            CommandId = Guid.NewGuid(),
            CommandName = "SampleCommand",
            Timestamp = DateTime.UtcNow
        };

        var options = new JsonSerializerOptions
        {
          PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
          DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
        };

        var payload = JsonSerializer.Serialize(command, options);

        var result = await _publisher.PublishEventAsync("my-pubsub", "my-topic", payload);
        if (!result.IsSuccess)
        {
            // Handle error
        }
    }

    public async Task SetStateAsync()
    {
        var saveResult = await _stateStore.SetStateAsync("my-store", "my-key", "my-value");
        if (!saveResult.IsSuccess)
        {
            // Handle error
        }
    }

    public async Task<string?> GetStateAsync()
    {
        var stateResult = await _stateStore.GetStateAsync<string>("my-store", "my-key");
        return stateResult.IsSuccess ? stateResult.Value : null;
    }

    public async Task DeleteStateAsync()
    {
        var deleteResult = await _stateStore.DeleteStateAsync("my-store", "my-key");
        if (!deleteResult.IsSuccess)
        {
            // Handle error
        }
    }
}

This setup enables your ASP.NET Core application to utilize Dapr's pub-sub, state management, and other building blocks with minimal boilerplate.

HA work leases (multi-replica)

Use Dapr state (not product DB lock tables) for bootstrap/sweep/holder coordination. The state Component is infrastructure (e.g. persistent NATS JetStream KV); application code stays broker-agnostic.

builder.Services.RegisterWorkLeases();

// inject IDaprWorkLeaseStore + IDaprRuntimeInstanceId
var acquired = await leases.TryAcquireAsync(
  storeName: "maksit-cicd-state",
  workKey: "bootstrap",
  holderId: runtimeInstance.InstanceId,
  ttl: TimeSpan.FromMinutes(5),
  ct);

if (acquired is { IsSuccess: true, Value: true }) {
  try { /* exclusive work */ }
  finally { await leases.ReleaseAsync("maksit-cicd-state", "bootstrap", runtimeInstance.InstanceId, CancellationToken.None); }
}

Pub/sub workers: implement IDaprPubSubWorkHandler<T> and map with DaprPubSubAck.ToActionResult (2xx ACK, 503 Busy → redelivery).

Contributing

Contributions to this project are welcome! Please fork the repository and submit a pull request with your changes. If you encounter any issues or have feature requests, feel free to open an issue on GitHub.

Contact

If you have any questions or need further assistance, feel free to reach out:

License

See LICENSE.md.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.2.2 119 8/16/2026
2.2.1 151 8/14/2026
2.2.0 184 8/1/2026
2.1.0 103 7/30/2026
2.0.1 167 6/28/2026
2.0.0 191 2/27/2026
1.0.8 203 11/2/2025
1.0.7 288 5/25/2025
1.0.6 223 5/5/2025
1.0.5 216 5/5/2025
1.0.4 188 11/14/2024