public ISagaScopeContext <T> GetScope <T>(ConsumeContext <T> context) where T : class { if (context.TryGetPayload <Scope>(out var existingScope)) { existingScope.UpdateScope(context); return(new ExistingSagaScopeContext <T>(context)); } var scope = AsyncScopedLifestyle.BeginScope(_container); try { scope.UpdateScope(context); var proxy = new ConsumeContextProxy <T>(context, new PayloadCacheScope(context)); proxy.UpdatePayload(scope); foreach (Action <ConsumeContext> scopeAction in _scopeActions) { scopeAction(proxy); } return(new CreatedSagaScopeContext <Scope, T>(scope, proxy)); } catch { scope.Dispose(); throw; } }
ISagaScopeContext <T> ISagaScopeProvider <TSaga> .GetScope <T>(ConsumeContext <T> context) { if (context.TryGetPayload <ILifetimeScope>(out _)) { return(new ExistingSagaScopeContext <T>(context)); } var parentLifetimeScope = _scopeProvider.GetLifetimeScope(context); var lifetimeScope = parentLifetimeScope.BeginLifetimeScope(_name, builder => { builder.ConfigureScope(context); _configureScope?.Invoke(builder, context); }); try { var proxy = new ConsumeContextProxy <T>(context, new PayloadCacheScope(context)); proxy.UpdatePayload(lifetimeScope); foreach (Action <ConsumeContext> scopeAction in _scopeActions) { scopeAction(proxy); } return(new CreatedSagaScopeContext <ILifetimeScope, T>(lifetimeScope, proxy)); } catch { lifetimeScope.Dispose(); throw; } }
public ISagaScopeContext <T> GetScope <T>(ConsumeContext <T> context) where T : class { if (context.TryGetPayload <IKernel>(out var kernel)) { kernel.UpdateScope(context); return(new ExistingSagaScopeContext <T>(context)); } var scope = _kernel.CreateNewOrUseExistingMessageScope(); try { _kernel.UpdateScope(context); var proxy = new ConsumeContextProxy <T>(context, new PayloadCacheScope(context)); proxy.UpdatePayload(_kernel); foreach (Action <ConsumeContext> scopeAction in _scopeActions) { scopeAction(proxy); } return(new CreatedSagaScopeContext <IDisposable, T>(scope, proxy)); } catch { scope.Dispose(); throw; } }
public ISagaScopeContext <T> GetScope <T>(ConsumeContext <T> context) where T : class { if (context.TryGetPayload <INestedContainer>(out var existingNestedContainer)) { existingNestedContainer.Inject <ConsumeContext>(context); return(new ExistingSagaScopeContext <T>(context)); } var nestedContainer = _container.GetNestedContainer(context); try { var proxy = new ConsumeContextProxy <T>(context, new PayloadCacheScope(context)); proxy.UpdatePayload(nestedContainer); foreach (Action <ConsumeContext> scopeAction in _scopeActions) { scopeAction(proxy); } return(new CreatedSagaScopeContext <INestedContainer, T>(nestedContainer, proxy)); } catch { nestedContainer.Dispose(); throw; } }
ISagaScopeContext <T> ISagaScopeProvider <TSaga> .GetScope <T>(ConsumeContext <T> context) { if (context.TryGetPayload <IServiceScope>(out var existingServiceScope)) { existingServiceScope.UpdateScope(context); return(new ExistingSagaScopeContext <T>(context)); } if (!context.TryGetPayload(out IServiceProvider serviceProvider)) { serviceProvider = _serviceProvider; } var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope(); try { serviceScope.UpdateScope(context); var proxy = new ConsumeContextProxy <T>(context, new PayloadCacheScope(context)); proxy.UpdatePayload(serviceScope); foreach (Action <ConsumeContext> scopeAction in _scopeActions) { scopeAction(proxy); } return(new CreatedSagaScopeContext <IServiceScope, T>(serviceScope, proxy)); } catch { serviceScope.Dispose(); throw; } }