public void BuildHandler(IApplicationBuilder app, ISerialize serialize) { RequestDelegateFactory.CreateDelegate(logger, messageHandler, out List <SubscribeModel> subDelegate).ForEach(x => { app.Map(x.Path, handle => { handle.MapWhen(p => p.Request.Method.Equals("POST"), builder => { builder.Run(async ctx => { using var lifetimescope = Container.BeginLifetimeScope();//每次从静态根容器引用构造一个独立的生命周期范围 await x.Excute(ctx, lifetimescope); }); }); }); }); if (subDelegate.Any()) { app.Map("/dapr/subscribe", handle => handle.Run(async ctx => { ctx.Response.ContentType = "application/json"; await ctx.Response.WriteAsync(serialize.SerializesJson(subDelegate)); })); } }
public ISharingLifetimeScope FindScope(ISharingLifetimeScope mostNestedVisibleScope) { #if NetCore var context = _container.GetInstance <IHttpContextAccessor>()?.HttpContext; if (context == null) { return(null); } if (!context.Items.ContainsKey(typeof(IComponentLifetime))) { lock (context) { if (!context.Items.ContainsKey(typeof(IComponentLifetime))) { var scope = (ISharingLifetimeScope)mostNestedVisibleScope.BeginLifetimeScope(mostNestedVisibleScope.ComponentRegistry); context.Items[typeof(IComponentLifetime)] = scope; return(scope); } } } return((ISharingLifetimeScope)context.Items[typeof(IComponentLifetime)]); #else var context = System.Web.HttpContext.Current; if (context == null) { return(null); } if (!context.Items.Contains(typeof(IComponentLifetime))) { lock (context.Items.SyncRoot) { if (!context.Items.Contains(typeof(IComponentLifetime))) { var scope = (ISharingLifetimeScope)mostNestedVisibleScope.BeginLifetimeScope(mostNestedVisibleScope.ComponentRegistry); context.Items[typeof(IComponentLifetime)] = scope; context.DisposeOnPipelineCompleted(scope); return(scope); } } } return((ISharingLifetimeScope)context.Items[typeof(IComponentLifetime)]); #endif }
public async Task SubscribeHandleInvoke() { await foreach (var message in Pipline.Reader.ReadAllAsync()) { try { using var lifescope = Container.BeginLifetimeScope(); await EventHandler(message, lifescope); } catch (Exception e) { logger.LogError($"进程内订阅器消息处理异常:{e.Message},调用堆栈:{e.StackTrace}"); } } }