百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

Chronicle

> 编程语言
开源

在 .NET Core 中实现 Saga 设计模式

397 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

在 .NET Core 中实现 Saga 设计模式

Chronicle

Chronicle is simple process manager/saga pattern implementation for .NET Core that helps you manage long-living and distirbuted transactions.

master develop
AppVeyor
CodeCov

Installation

Chornicle is available on NuGet

Package manager

bash
Install-Package Chronicle_ -Version 3.2.1

.NET CLI

bash
dotnet add package Chronicle_ --version 3.2.1

Getting started

In order to create and process a saga you need to go through a few steps:

  1. Create a class that dervies from either Saga or Saga.
  2. Inside your saga implemention, inherit from one or several ISagaStartAction and ISagaAction to implement HandleAsync() and CompensateAsync() methods for each message type. An initial step must be implemented as an ISagaStartAction, while the rest can be ISagaAction. It's worth mentioning that you can implement as many ISagaStartAction as you want. In this case, the first incoming message is going to initialize the saga and any subsequent ISagaStartAction or ISagaAction will only update the current saga state.
  3. Register all your sagas in Startup.cs by calling services.AddChronicle(). By default, AddChronicle() will use the InMemorySagaStateRepository and InMemorySagaLog for maintaining SagaState and for logging SagaLogData in the SagaLog. The SagaLog maintains a historical record of which message handlers have been executed. Optionally, AddChronicle() accepts an Action parameter which provides access to UseSagaStateRepository() and UseSagaLog() for custom implementations of ISagaStateRepository and ISagaLog. If either method is called, then both methods need to be called.
  4. Inject ISagaCoordinator and invoke ProcessAsync() methods passing a message. The coordinator will take care of everything by looking for all implemented sagas that can handle a given message.
  5. To complete a successful saga, call CompleteSaga() or CompleteSagaAsync(). This will update the SagaState to Completed. To flag a saga which has failed or been rejected, call the Reject() or RejectAsync() methods to update the SagaState to Rejected. Doing so will utilize the SagaLog to call each message type's CompensateAsync() in the reverse order of their respective HandleAsync() method was called. Additionally, an unhanded exception thrown from a HandleAsync() method will cause Reject() to be called and begin the compensation.

Below is the very simple example of saga that completes once both messages (Message1 and Message2) are received:

…

Both messages are processed by mentioned coordinator:

csharp
var coordinator = app.ApplicationServices.GetService();

var context = SagaContext
    .Create()
    .WithCorrelationId(Guid.NewGuid())
    .Build();

coordinator.ProcessAsync(new Message1 { Text = "Hello" }, context);
coordinator.ProcessAsync(new Message2 { Text = "World" }, context);

The result looks as follows:

Documentation

If you're looking for documentation, you can find it here.

Icon

Icon made by Smashicons from www.flaticon.com is licensed by Creative Commons BY 3.0

Issues· 23 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言