public async Task Invoke(HttpContext httpContext) { //每次请求将重新初始化全局容器确保容器唯一 OxygenIocContainer.BuilderIocContainer(container); //为客户端信息添加追踪头 OxygenIocContainer.Resolve <CustomerInfo>().SetTraceHeader(TraceHeaderHelper.GetTraceHeaders(httpContext.Request.Headers)); await _next(httpContext); }
internal override async Task Excute(HttpContext ctx, ILifetimeScope scope) { OxygenIocContainer.BuilderIocContainer(scope);//仅在当前请求内创建上下文模型 byte[] result = new byte[0]; ctx.Response.ContentType = "application/json"; var messageType = MessageType.Json; try { if (ctx.Request.ContentType == "application/x-msgpack") { ctx.Response.ContentType = "application/x-msgpack"; messageType = MessageType.MessagePack; } if (ctx.Request.ContentType == null) { ctx.Response.ContentType = "text/html"; messageType = MessageType.Html; } HttpContextExtension.ContextWapper.Value = new OxygenHttpContextWapper(Path, scope, ctx.Request.Headers.GetHeaderDictionary(), ctx.Request.Cookies.GetCookieDictionary(), ctx.Response); Tout localCallbackResult = null; if (noInput) { localCallbackResult = await LocalMethodAopProvider.UsePipelineHandler(scope, HttpContextExtension.ContextWapper.Value, NoInputMethodDelegate); } else { var messageobj = await messageHandler.ParseMessage <Tin>(ctx, messageType); if (messageobj == default(Tin)) { throw new FormatException($"参数反序列化失败,接口地址{Path},入参类型:{typeof(Tin).Name}"); } localCallbackResult = await LocalMethodAopProvider.UsePipelineHandler(scope, messageobj, HttpContextExtension.ContextWapper.Value, MethodDelegate); } if (localCallbackResult != null) { result = messageHandler.BuildMessage(localCallbackResult, messageType); } } catch (Exception e) { logger.LogError($"服务端消息处理异常: {e.GetBaseException()?.Message ?? e.Message}"); ctx.Response.StatusCode = 502; if (e is FormatException) { await ctx.Response.Body.WriteAsync(Encoding.UTF8.GetBytes(e.Message)); } } finally { await ctx.Response.Body.WriteAsync(result, 0, result.Length); OxygenIocContainer.DisposeIocContainer();//注销上下文 } }
internal static void CreateDelegate(ActorRegistrationCollection actorRegistrations) { OxygenIocContainer.BuilderIocContainer(_lifetimeScope); //获取所有标记为remote的servie var remoteservice = ReflectionHelper.GetTypesByAttributes(true, typeof(RemoteServiceAttribute)); //获取所有标记为remote的method构造具体的delegate remoteservice.ToList().ForEach(x => { var implType = ReflectionHelper.GetTypeByInterface(x); if (implType != null) { var methods = new List <MethodInfo>(); ReflectionHelper.GetMethodByFilter(x, typeof(RemoteFuncAttribute)).ToList().ForEach(y => { var funcAttr = ReflectionHelper.GetAttributeProperyiesByMethodInfo <RemoteFuncAttribute>(y); //生成服务调用代理 if (funcAttr.FuncType == FuncType.Actor) { methods.Add(y); } }); if (methods.Any()) { try { var typeBuilder = ActorProxyBuilder.GetType(x, implType, methods.ToArray()); typeof(ActorRegistrationCollection).GetMethod("RegisterActor").MakeGenericMethod(typeBuilder.proxyType).Invoke(actorRegistrations, new object[] { default(Action <ActorRegistration>) }); _lifetimeScope.Resolve <ISubscribeInProcessFactory>().RegisterEventHandler(implType.BaseType.GetProperty("ActorData").PropertyType.FullName, _lifetimeScope, typeBuilder.SaveDataFunc); } catch (Exception e) { _lifetimeScope.Resolve <ILogger>().LogError($"Actor代理创建失败,原因:{e.GetBaseException().Message}"); } } } }); }
public OxygenHostService(ILifetimeScope lifetimeScope) { OxygenIocContainer.BuilderIocContainer(lifetimeScope); RemoteProxyGenerator.InitRemoteMessageSenderDelegate();//初始化消息发送代理 }