添加项目文件。

This commit is contained in:
2026-05-09 17:06:30 +08:00
parent c60f5fe117
commit 720ef957d4
378 changed files with 14843 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
namespace IM.InitCommon
{
public static class GrpcExtension
{
public static IServiceCollection AddAllGrpcServer(this IServiceCollection services)
{
services.AddGrpc(options =>
{
// 开启详细错误(开发环境很有用,生产环境可结合配置读取)
options.EnableDetailedErrors = true;
// 限制最大接收和发送的消息大小 (例如 10MB,防止大包攻击)
options.MaxReceiveMessageSize = 10 * 1024 * 1024;
options.MaxSendMessageSize = 10 * 1024 * 1024;
// TODO: 未来你可以在这里添加全局异常拦截器 (Interceptor)
// options.Interceptors.Add<GlobalGrpcExceptionInterceptor>();
});
return services;
}
public static IEndpointRouteBuilder MapAllGrpcServer(this IEndpointRouteBuilder endpoints)
{
// 获取调用此方法的程序集(即具体的微服务项目,如 MessageService
var assembly = Assembly.GetCallingAssembly();
// 获取 MapGrpcService<T> 的方法反射信息
var mapGrpcServiceMethod = typeof(GrpcEndpointRouteBuilderExtensions)
.GetMethods(BindingFlags.Static | BindingFlags.Public)
.First(m => m.Name == "MapGrpcService" && m.GetGenericArguments().Length == 1);
// 查找当前项目中所有继承了 gRPC 生成的 Base 类的具体实现类
// gRPC 生成的基类通常以 "Base" 结尾,例如 ConversationInternalBase
var grpcTypes = assembly.GetTypes()
.Where(t => t.IsClass
&& !t.IsAbstract
&& t.BaseType != null
&& t.BaseType.Name.EndsWith("Base"))
.ToList();
// 循环并动态调用 MapGrpcService<T>
foreach (var type in grpcTypes)
{
var genericMethod = mapGrpcServiceMethod.MakeGenericMethod(type);
genericMethod.Invoke(null, new object[] { endpoints });
}
return endpoints;
}
}
}