Browse Source
Merge pull request #807 from colinin/support-idempotent
feat(idempotent): add IgnoreIdempotentAttribute
pull/808/head
yx lin
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with
23 additions and
1 deletions
-
aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IIdempotentChecker.cs
-
aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentAttribute.cs
-
aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentChecker.cs
-
aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentDeniedHandler.cs
-
aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentInterceptor.cs
-
aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IdempotentKeyNormalizer.cs
-
aspnet-core/modules/common/LINGYUN.Abp.Idempotent/LINGYUN/Abp/Idempotent/IgnoreIdempotentAttribute.cs
-
aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Idempotent.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Idempotent/Wrapper/IdempotentHttpResponseWrapper.cs
-
aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Idempotent/LINGYUN/Abp/AspNetCore/Mvc/Idempotent/AbpIdempotentActionFilter.cs
|
|
|
@ -1,6 +1,7 @@ |
|
|
|
using System.Threading.Tasks; |
|
|
|
|
|
|
|
namespace LINGYUN.Abp.Idempotent; |
|
|
|
|
|
|
|
public interface IIdempotentChecker |
|
|
|
{ |
|
|
|
Task CheckAsync(IdempotentCheckContext context); |
|
|
|
|
|
|
|
@ -30,17 +30,19 @@ public class IdempotentAttribute : Attribute |
|
|
|
/// <summary>
|
|
|
|
/// 自定义的幂等key
|
|
|
|
/// </summary>
|
|
|
|
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; |
|
|
|
|
|
|
|
@ -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<IdempotentAttribute>(); |
|
|
|
|
|
|
|
var methodLockTimeout = _idempotentOptions.DefaultTimeout; |
|
|
|
|
|
|
|
@ -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) |
|
|
|
|
|
|
|
@ -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); |
|
|
|
|
|
|
|
@ -18,6 +18,7 @@ public class IdempotentKeyNormalizer : IIdempotentKeyNormalizer, ITransientDepen |
|
|
|
_jsonSerializer = jsonSerializer; |
|
|
|
} |
|
|
|
|
|
|
|
[IgnoreIdempotent] |
|
|
|
public virtual string NormalizeKey(IdempotentKeyNormalizerContext context) |
|
|
|
{ |
|
|
|
var methodIdBuilder = new StringBuilder(); |
|
|
|
|
|
|
|
@ -0,0 +1,8 @@ |
|
|
|
using System; |
|
|
|
|
|
|
|
namespace LINGYUN.Abp.Idempotent; |
|
|
|
|
|
|
|
[AttributeUsage(AttributeTargets.Method)] |
|
|
|
public class IgnoreIdempotentAttribute : Attribute |
|
|
|
{ |
|
|
|
} |
|
|
|
@ -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) |
|
|
|
|
|
|
|
@ -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)) |
|
|
|
|