From 8c2178016ac57a41c760bc35c0fe162f15d71c71 Mon Sep 17 00:00:00 2001
From: cKey <35512826+colinin@users.noreply.github.com>
Date: Tue, 21 Mar 2023 11:13:30 +0800
Subject: [PATCH] feat(idempotent): add IgnoreIdempotentAttribute
---
.../LINGYUN/Abp/Idempotent/IIdempotentChecker.cs | 1 +
.../LINGYUN/Abp/Idempotent/IdempotentAttribute.cs | 4 +++-
.../LINGYUN/Abp/Idempotent/IdempotentChecker.cs | 6 ++++++
.../LINGYUN/Abp/Idempotent/IdempotentDeniedHandler.cs | 1 +
.../LINGYUN/Abp/Idempotent/IdempotentInterceptor.cs | 1 +
.../LINGYUN/Abp/Idempotent/IdempotentKeyNormalizer.cs | 1 +
.../LINGYUN/Abp/Idempotent/IgnoreIdempotentAttribute.cs | 8 ++++++++
.../Idempotent/Wrapper/IdempotentHttpResponseWrapper.cs | 1 +
.../Mvc/Idempotent/AbpIdempotentActionFilter.cs | 1 +
9 files changed, 23 insertions(+), 1 deletion(-)
create mode 100644 aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IgnoreIdempotentAttribute.cs
diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IIdempotentChecker.cs b/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IIdempotentChecker.cs
index d0849d764..15395d3b8 100644
--- a/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IIdempotentChecker.cs
+++ b/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IIdempotentChecker.cs
@@ -1,6 +1,7 @@
using System.Threading.Tasks;
namespace LINGYUN.Abp.Idempotent;
+
public interface IIdempotentChecker
{
Task CheckAsync(IdempotentCheckContext context);
diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentAttribute.cs b/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentAttribute.cs
index 09b36e5ce..fe9c16792 100644
--- a/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentAttribute.cs
+++ b/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentAttribute.cs
@@ -30,17 +30,19 @@ public class IdempotentAttribute : Attribute
///
/// 自定义的幂等key
///
- public string? IdempotentKey { get; }
+ public string? IdempotentKey { get; set; }
public IdempotentAttribute()
{
}
public IdempotentAttribute(
+ string? iodempotentKey = null,
int? timeout = null,
string? redirectUrl = null,
string[]? keyMap = null)
{
+ IdempotentKey = iodempotentKey;
Timeout = timeout;
KeyMap = keyMap;
RedirectUrl = redirectUrl;
diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentChecker.cs b/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentChecker.cs
index a7bfaf791..d37978e23 100644
--- a/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentChecker.cs
+++ b/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentChecker.cs
@@ -23,6 +23,7 @@ public class IdempotentChecker : IIdempotentChecker, ITransientDependency
_idempotentDeniedHandler = idempotentDeniedHandler;
}
+ [IgnoreIdempotent]
public async virtual Task CheckAsync(IdempotentCheckContext context)
{
if (!_idempotentOptions.IsEnabled)
@@ -30,6 +31,11 @@ public class IdempotentChecker : IIdempotentChecker, ITransientDependency
return;
}
+ if (context.Method.IsDefined(typeof(IgnoreIdempotentAttribute), true))
+ {
+ return;
+ }
+
var attr = context.Method.GetCustomAttribute();
var methodLockTimeout = _idempotentOptions.DefaultTimeout;
diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentDeniedHandler.cs b/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentDeniedHandler.cs
index 517c48467..a43a66f01 100644
--- a/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentDeniedHandler.cs
+++ b/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentDeniedHandler.cs
@@ -6,6 +6,7 @@ namespace LINGYUN.Abp.Idempotent;
public class IdempotentDeniedHandler : IIdempotentDeniedHandler, ISingletonDependency
{
+ [IgnoreIdempotent]
public virtual void Denied(IdempotentDeniedContext context)
{
var exception = new IdempotentDeniedException(context.IdempotentKey, IdempotentErrorCodes.IdempotentDenied)
diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentInterceptor.cs b/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentInterceptor.cs
index 4cceef3d6..f18f91176 100644
--- a/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentInterceptor.cs
+++ b/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentInterceptor.cs
@@ -17,6 +17,7 @@ public class IdempotentInterceptor : AbpInterceptor, ITransientDependency
_idempotentKeyNormalizer = idempotentKeyNormalizer;
}
+ [IgnoreIdempotent]
public async override Task InterceptAsync(IAbpMethodInvocation invocation)
{
var targetType = ProxyHelper.GetUnProxiedType(invocation.TargetObject);
diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentKeyNormalizer.cs b/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentKeyNormalizer.cs
index dbd32af04..d9eb7707d 100644
--- a/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentKeyNormalizer.cs
+++ b/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentKeyNormalizer.cs
@@ -18,6 +18,7 @@ public class IdempotentKeyNormalizer : IIdempotentKeyNormalizer, ITransientDepen
_jsonSerializer = jsonSerializer;
}
+ [IgnoreIdempotent]
public virtual string NormalizeKey(IdempotentKeyNormalizerContext context)
{
var methodIdBuilder = new StringBuilder();
diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IgnoreIdempotentAttribute.cs b/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IgnoreIdempotentAttribute.cs
new file mode 100644
index 000000000..73595f6e2
--- /dev/null
+++ b/aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IgnoreIdempotentAttribute.cs
@@ -0,0 +1,8 @@
+using System;
+
+namespace LINGYUN.Abp.Idempotent;
+
+[AttributeUsage(AttributeTargets.Method)]
+public class IgnoreIdempotentAttribute : Attribute
+{
+}
diff --git a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Idempotent.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Idempotent/Wrapper/IdempotentHttpResponseWrapper.cs b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Idempotent.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Idempotent/Wrapper/IdempotentHttpResponseWrapper.cs
index e9392129d..ce37622f4 100644
--- a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Idempotent.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Idempotent/Wrapper/IdempotentHttpResponseWrapper.cs
+++ b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Idempotent.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Idempotent/Wrapper/IdempotentHttpResponseWrapper.cs
@@ -21,6 +21,7 @@ public class IdempotentHttpResponseWrapper : HttpResponseWrapper, ITransientDepe
IdempotentOptions = idempotentOptions.Value;
}
+ [IgnoreIdempotent]
public override void Wrap(HttpResponseWrapperContext context)
{
if (context.HttpContext.Items.TryGetValue(nameof(IdempotentAttribute.RedirectUrl), out var redirectUrl) && redirectUrl != null)
diff --git a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Idempotent/LINGYUN/Abp/AspNetCore/Mvc/Idempotent/AbpIdempotentActionFilter.cs b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Idempotent/LINGYUN/Abp/AspNetCore/Mvc/Idempotent/AbpIdempotentActionFilter.cs
index 33e5f6359..e8237ebd1 100644
--- a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Idempotent/LINGYUN/Abp/AspNetCore/Mvc/Idempotent/AbpIdempotentActionFilter.cs
+++ b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Idempotent/LINGYUN/Abp/AspNetCore/Mvc/Idempotent/AbpIdempotentActionFilter.cs
@@ -12,6 +12,7 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Idempotent;
public class AbpIdempotentActionFilter : IAsyncActionFilter, ITransientDependency
{
+ [IgnoreIdempotent]
public async virtual Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
if (!ShouldCheckIdempotent(context))