Browse Source

feat(docs): 添加接口幂等性检查模块文档

pull/1049/head
feijie 1 year ago
parent
commit
858112ac80
  1. 98
      aspnet-core/framework/common/LINGYUN.Abp.Idempotent/README.EN.md
  2. 98
      aspnet-core/framework/common/LINGYUN.Abp.Idempotent/README.md

98
aspnet-core/framework/common/LINGYUN.Abp.Idempotent/README.EN.md

@ -0,0 +1,98 @@
# LINGYUN.Abp.Idempotent
Interface idempotency check module for preventing duplicate submissions and ensuring interface call idempotency.
## Features
* Automatic idempotency checking
* Support for custom idempotent key generation
* Flexible timeout configuration
* Distributed lock support
* Multi-language error messages
* Support for ignoring specific interfaces or methods
## Installation
```bash
dotnet add package LINGYUN.Abp.Idempotent
```
## Configuration
```csharp
[DependsOn(typeof(AbpIdempotentModule))]
public class YouProjectModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpIdempotentOptions>(options =>
{
// Enable idempotency check globally
options.IsEnabled = true;
// Default 5-second timeout for each interface
options.DefaultTimeout = 5000;
// Idempotent token name, passed through HttpHeader
options.IdempotentTokenName = "X-With-Idempotent-Token";
// HTTP status code when idempotency check fails
options.HttpStatusCode = 429;
});
}
}
```
## Configuration Options
* `IsEnabled` - Whether to enable idempotency checking, default: false
* `DefaultTimeout` - Default idempotency check timeout (milliseconds), default: 5000
* `IdempotentTokenName` - HTTP header name for idempotency token, default: X-With-Idempotent-Token
* `HttpStatusCode` - HTTP status code when idempotency check fails, default: 429 (Too Many Requests)
## Usage Examples
### 1. Basic Usage
```csharp
[Idempotent]
public class OrderAppService : ApplicationService
{
public async Task<OrderDto> CreateAsync(CreateOrderDto input)
{
// Method will automatically perform idempotency check
return await _orderRepository.CreateAsync(input);
}
}
```
### 2. Custom Idempotent Key
```csharp
[Idempotent(
iodempotentKey: "custom-key",
timeout: 10000,
keyMap: new[] { "orderId", "userId" })]
public async Task<OrderDto> UpdateAsync(UpdateOrderDto input)
{
return await _orderRepository.UpdateAsync(input);
}
```
### 3. Ignore Idempotency Check
```csharp
[IgnoreIdempotent]
public async Task<OrderDto> QueryAsync(string orderId)
{
return await _orderRepository.GetAsync(orderId);
}
```
## Important Notes
1. By default, idempotency checking is enabled for all services inheriting from `ICreateAppService`, `IUpdateAppService`, and `IDeleteAppService`
2. You can use the `[IgnoreIdempotent]` attribute to ignore idempotency checking for specific methods
3. Idempotency checking is implemented based on distributed locks to ensure correctness in distributed environments
4. It is recommended to enable idempotency checking on all interfaces that modify data
## Links
* [中文文档](./README.md)

98
aspnet-core/framework/common/LINGYUN.Abp.Idempotent/README.md

@ -1,6 +1,21 @@
# LINGYUN.Abp.Idempotent
接口幂等性检查模块
接口幂等性检查模块,用于防止接口重复提交和确保接口调用的幂等性。
## 功能
* 自动幂等性检查
* 支持自定义幂等键生成
* 灵活的超时配置
* 支持分布式锁
* 支持多语言错误消息
* 支持忽略特定接口或方法
## 安装
```bash
dotnet add package LINGYUN.Abp.Idempotent
```
## 配置使用
@ -8,23 +23,76 @@
[DependsOn(typeof(AbpIdempotentModule))]
public class YouProjectModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpIdempotentOptions>(options =>
{
// 全局启用幂等检查
options.IsEnabled = true;
// 默认每个接口提供5秒超时
options.DefaultTimeout = 5000;
// 幂等token名称, 通过HttpHeader传递
options.IdempotentTokenName = "X-With-Idempotent-Token";
// 幂等校验失败时Http响应代码
options.HttpStatusCode = 429;
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpIdempotentOptions>(options =>
{
// 全局启用幂等检查
options.IsEnabled = true;
// 默认每个接口提供5秒超时
options.DefaultTimeout = 5000;
// 幂等token名称, 通过HttpHeader传递
options.IdempotentTokenName = "X-With-Idempotent-Token";
// 幂等校验失败时Http响应代码
options.HttpStatusCode = 429;
});
}
}
}
```
## 配置项说明
## 其他
* `IsEnabled` - 是否启用幂等检查,默认:false
* `DefaultTimeout` - 默认幂等性检查超时时间(毫秒),默认:5000
* `IdempotentTokenName` - 幂等性Token的HTTP头名称,默认:X-With-Idempotent-Token
* `HttpStatusCode` - 幂等性检查失败时的HTTP状态码,默认:429 (Too Many Requests)
## 使用示例
### 1. 基本使用
```csharp
[Idempotent]
public class OrderAppService : ApplicationService
{
public async Task<OrderDto> CreateAsync(CreateOrderDto input)
{
// 方法会自动进行幂等性检查
return await _orderRepository.CreateAsync(input);
}
}
```
### 2. 自定义幂等键
```csharp
[Idempotent(
iodempotentKey: "custom-key",
timeout: 10000,
keyMap: new[] { "orderId", "userId" })]
public async Task<OrderDto> UpdateAsync(UpdateOrderDto input)
{
return await _orderRepository.UpdateAsync(input);
}
```
### 3. 忽略幂等性检查
```csharp
[IgnoreIdempotent]
public async Task<OrderDto> QueryAsync(string orderId)
{
return await _orderRepository.GetAsync(orderId);
}
```
## 注意事项
1. 默认对所有继承自 `ICreateAppService`、`IUpdateAppService` 和 `IDeleteAppService` 的服务启用幂等性检查
2. 可以通过 `[IgnoreIdempotent]` 特性来忽略特定方法的幂等性检查
3. 幂等性检查基于分布式锁实现,确保在分布式环境中的正确性
4. 建议在所有修改数据的接口上启用幂等性检查
## 链接
* [English document](./README.EN.md)

Loading…
Cancel
Save