XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension.NetCore.ServerInteractive

[DLL] Server interaction extension, including user identity verification and querying in conjunction with AutoConfig

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension.NetCore.ServerInteractive

fix: AddServiceWithRoute supports wildcards, enforce uniqueness, normalize paths

- AddServiceWithRoute<T>() now routes wildcard patterns to WildcardCoreServiceList - Both AddService<T>() and AddServiceWithRoute<T>() throw on duplicate wildcard patterns - Source generator normalizes paths with Trim('/') before validation - Fix corrupted merge in XFEServerCore.cs Agent-Logs-Url: https://github.com/XFEstudio/XFEExtension.NetCore.ServerInteractive/sessions/5a64aff0-11ed-41a3-ba6a-365d436dd98e Co-authored-by: XFEstudio <132526994+XFEstudio@users.noreply.github.com>

cfcba98
copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
提交于

代码差异

3 个文件 +47 -46
Modified XFEExtension.NetCore.ServerInteractive.SourceGenerator/EntryPointGenerator.cs +2 -0
@@ -140,6 +140,8 @@ public class EntryPointGenerator : IIncrementalGenerator
140 140 // 空路径或 "*" 均视为全匹配通配符
141 141 if (string.IsNullOrEmpty(path))
142 142 path = "*";
143 else
144 path = path.Trim('/');
143 145
144 146 results.Add(new MethodCandidate(
145 147 containingType.ContainingNamespace.ToDisplayString(),
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/XFEServerCore.cs +30 -44
@@ -142,46 +142,11 @@ public abstract class XFEServerCore : ServerCoreServiceBase
142 142
143 143 var segments = url.Segments.Skip(1).Select(s => s.TrimEnd('/')).Where(s => !string.IsNullOrEmpty(s)).ToArray();
144 144
145 // 在字典中查找对应的服务(先精确匹配,再通配符匹配)
146 Func<IServerCoreStandardService>? serviceFactory = null;
147 string? matchedPattern = null;
148
149 if (StandardCoreServiceDictionary.TryGetValue(route, out serviceFactory))
150 {
151 matchedPattern = route;
152 }
153 else
154 {
155 // 尝试通配符匹配
156 foreach (var (pattern, factory) in WildcardCoreServiceList)
157 {
158 if (RouteMatchHelper.MatchWildcardRoute(pattern, route))
159 {
160 matchedPattern = pattern;
161 serviceFactory = factory;
162 break;
163 }
164 }
165 }
166
167 if (serviceFactory is not null && matchedPattern is not null)
168 {
169 var serviceInstance = serviceFactory();
170 try
145 // 如果MainEntryPoint不为空,则需要匹配主入口点
146 string route;
147 if (!string.IsNullOrEmpty(MainEntryPoint))
171 148 {
172 serviceInstance.XFEServerCore = this;
173 serviceInstance.Route = route;
174 serviceInstance.Json = queryableJsonNode;
175 serviceInstance.Request = e.Request;
176 serviceInstance.ReturnArgs = r;
177 serviceInstance.Initialize();
178
179 // 根据匹配的模式调用对应的处理方法(同步与异步互斥,优先同步)
180 if (serviceInstance.SyncEntryPoints.TryGetValue(matchedPattern, out var syncHandler))
181 syncHandler();
182 else if (serviceInstance.AsyncEntryPoints.TryGetValue(matchedPattern, out var asyncHandler))
183 await asyncHandler();
184 else
149 if (segments.Length == 0 || segments[0] != MainEntryPoint)
185 150 {
186 151 ServerCoreError?.Invoke(this, new()
187 152 {
@@ -214,8 +179,29 @@ public abstract class XFEServerCore : ServerCoreServiceBase
214 179 Console.Write($"({ServerCoreName})【{clientIP}】请求路由-{route}:");
215 180 var stopWatch = Stopwatch.StartNew();
216 181
217 // 在字典中查找对应的服务
218 if (StandardCoreServiceDictionary.TryGetValue(route, out var serviceFactory))
182 // 在字典中查找对应的服务(先精确匹配,再通配符匹配)
183 Func<IServerCoreStandardService>? serviceFactory = null;
184 string? matchedPattern = null;
185
186 if (StandardCoreServiceDictionary.TryGetValue(route, out serviceFactory))
187 {
188 matchedPattern = route;
189 }
190 else
191 {
192 // 尝试通配符匹配
193 foreach (var (pattern, factory) in WildcardCoreServiceList)
194 {
195 if (RouteMatchHelper.MatchWildcardRoute(pattern, route))
196 {
197 matchedPattern = pattern;
198 serviceFactory = factory;
199 break;
200 }
201 }
202 }
203
204 if (serviceFactory is not null && matchedPattern is not null)
219 205 {
220 206 var serviceInstance = serviceFactory();
221 207 try
@@ -227,10 +213,10 @@ public abstract class XFEServerCore : ServerCoreServiceBase
227 213 serviceInstance.ReturnArgs = r;
228 214 serviceInstance.Initialize();
229 215
230 // 根据路由调用对应的处理方法(同步与异步互斥,优先同步)
231 if (serviceInstance.SyncEntryPoints.TryGetValue(route, out var syncHandler))
216 // 根据匹配的模式调用对应的处理方法(同步与异步互斥,优先同步)
217 if (serviceInstance.SyncEntryPoints.TryGetValue(matchedPattern, out var syncHandler))
232 218 syncHandler();
233 else if (serviceInstance.AsyncEntryPoints.TryGetValue(route, out var asyncHandler))
219 else if (serviceInstance.AsyncEntryPoints.TryGetValue(matchedPattern, out var asyncHandler))
234 220 await asyncHandler();
235 221 else
236 222 {
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/XFEServerCoreBuilder.cs +15 -2
@@ -77,12 +77,23 @@ public abstract class XFEServerCoreBuilder : XFEBuilderBase<XFEServerCoreBuilder
77 77 ArgumentException.ThrowIfNullOrWhiteSpace(route, nameof(route));
78 78 route = route.Trim('/');
79 79
80 _serverStandardCoreServiceDictionary.Add(route, () =>
80 Func<IServerCoreStandardService> factory = () =>
81 81 {
82 82 var inst = new T();
83 83 ApplyParameter(inst);
84 84 return inst;
85 });
85 };
86
87 if (RouteMatchHelper.IsWildcardRoute(route))
88 {
89 if (_serverWildcardCoreServiceList.Any(entry => entry.Pattern == route))
90 throw new InvalidOperationException($"通配符路由 '{route}' 已重复注册(服务类型:{typeof(T).Name})");
91 _serverWildcardCoreServiceList.Add((route, factory));
92 }
93 else
94 {
95 _serverStandardCoreServiceDictionary.Add(route, factory);
96 }
86 97 return this;
87 98 }
88 99
@@ -116,6 +127,8 @@ public abstract class XFEServerCoreBuilder : XFEBuilderBase<XFEServerCoreBuilder
116 127
117 128 if (RouteMatchHelper.IsWildcardRoute(route))
118 129 {
130 if (_serverWildcardCoreServiceList.Any(entry => entry.Pattern == route))
131 throw new InvalidOperationException($"通配符路由 '{route}' 已重复注册(服务类型:{typeof(T).Name})");
119 132 _serverWildcardCoreServiceList.Add((route, factory));
120 133 }
121 134 else