From 783410c3397b889953ac361e72fd31eca0a5aab1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=BC=A0=E5=BF=97=E7=82=9C?=
<40768649+zhangzw218@users.noreply.github.com>
Date: Wed, 17 Sep 2025 21:50:27 +0800
Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E5=B1=80=E6=B3=A8=E5=86=8CIRpcActionF?=
=?UTF-8?q?ilter=20(#83)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/TouchSocket.Rpc/Common/RpcMethod.cs | 14 +++++-
src/TouchSocket.Rpc/Common/RpcStore.cs | 46 +++++++++++++++++++
.../InternalRpcServerProvider.cs | 2 +-
3 files changed, 59 insertions(+), 3 deletions(-)
diff --git a/src/TouchSocket.Rpc/Common/RpcMethod.cs b/src/TouchSocket.Rpc/Common/RpcMethod.cs
index a5b84ac79..00f6e1fb3 100644
--- a/src/TouchSocket.Rpc/Common/RpcMethod.cs
+++ b/src/TouchSocket.Rpc/Common/RpcMethod.cs
@@ -246,9 +246,11 @@ public sealed class RpcMethod : Method
///
/// 筛选器
///
- public IReadOnlyList GetFilters()
+ /// 全局筛选器
+ ///
+ public IReadOnlyList GetFilters(IReadOnlyList g_actionFilters)
{
- if (this.m_hasFilters[0] || this.m_hasFilters[1] || this.m_hasFilters[2] || this.m_hasFilters[3])
+ if (this.m_hasFilters[0] || this.m_hasFilters[1] || this.m_hasFilters[2] || this.m_hasFilters[3] || (g_actionFilters?.Any()) == true)
{
var actionFilters = new List();
//注册方法
@@ -297,6 +299,14 @@ public sealed class RpcMethod : Method
}
}
+ if (g_actionFilters?.Any() == true)
+ {
+ //全局筛选器
+ foreach (var filter in g_actionFilters)
+ {
+ this.AddActionFilter(filter, ref actionFilters);
+ }
+ }
return actionFilters;
}
return new IRpcActionFilter[0];
diff --git a/src/TouchSocket.Rpc/Common/RpcStore.cs b/src/TouchSocket.Rpc/Common/RpcStore.cs
index d668e97d4..cb5a3b4c8 100644
--- a/src/TouchSocket.Rpc/Common/RpcStore.cs
+++ b/src/TouchSocket.Rpc/Common/RpcStore.cs
@@ -108,8 +108,54 @@ public sealed class RpcStore
return this.m_serverTypes[serverType].ToArray();
}
+ #region 全局筛选器
+
+ private readonly ConcurrentList m_filters = new ConcurrentList();
+
+ ///
+ /// 获取全局筛选器对象
+ ///
+ ///
+ ///
+ public IReadOnlyList GetFilters(ICallContext callContext)
+ {
+ return m_filters.Select(s=>(IRpcActionFilter) callContext.Resolver.Resolve(s)).ToList();
+ }
+
+ ///
+ /// 添加全局筛选器
+ ///
+ ///
+ public void Filter() where TFilter : class, IRpcActionFilter
+ {
+ var filterType = typeof(TFilter);
+ this.Filter(filterType);
+ }
+
+ ///
+ /// 添加全局过滤器
+ ///
+ public void Filter(Type filterType)
+ {
+ if (!typeof(IRpcActionFilter).IsAssignableFrom(filterType))
+ {
+ throw new RpcException($"注册类型必须与{nameof(IRpcActionFilter)}有继承关系");
+ }
+
+ if (!m_filters.Any(s => s.FullName == filterType.FullName))
+ {
+ m_filters.Add(filterType);
+ this.m_registrator.RegisterTransient(filterType);
+ }
+ }
+
+
+ #endregion
+
+
#region 注册
+
///
/// 注册为单例服务
///
diff --git a/src/TouchSocket.Rpc/RpcServerProvider/InternalRpcServerProvider.cs b/src/TouchSocket.Rpc/RpcServerProvider/InternalRpcServerProvider.cs
index 2735a3032..6c87848cb 100644
--- a/src/TouchSocket.Rpc/RpcServerProvider/InternalRpcServerProvider.cs
+++ b/src/TouchSocket.Rpc/RpcServerProvider/InternalRpcServerProvider.cs
@@ -42,7 +42,7 @@ internal sealed class InternalRpcServerProvider : IRpcServerProvider
return new InvokeResult(InvokeStatus.UnFound);
}
- var filters = method.GetFilters();
+ var filters = method.GetFilters(m_rpcStore.GetFilters(callContext));
try
{
for (var i = 0; i < filters.Count; i++)