コード例 #1
0
        public IExecuteActivityScopeContext <TActivity, TArguments> GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <IUnityContainer>(out var existingContainer))
            {
                var activity = existingContainer.Resolve <TActivity>(new DependencyOverride(typeof(TArguments), new InjectionParameter(context.Arguments)));

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext));
            }

            var scope = _container.CreateChildContainer();

            try
            {
                var activity = scope.Resolve <TActivity>(new DependencyOverride(typeof(TArguments), new InjectionParameter(context.Arguments)));

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                var contextScope = scope;
                activityContext.GetOrAddPayload(() => contextScope);

                return(new CreatedExecuteActivityScopeContext <IUnityContainer, TActivity, TArguments>(contextScope, activityContext));
            }
            catch
            {
                scope.Dispose();

                throw;
            }
        }
コード例 #2
0
        public IExecuteActivityScopeContext <TActivity, TArguments> GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <IContainer>(out var existingContainer))
            {
                existingContainer.Inject <ConsumeContext>(context);

                var activity = existingContainer
                               .With(context.Arguments)
                               .GetInstance <TActivity>();

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext));
            }

            var nestedContainer = _container?.CreateNestedContainer(context) ?? _context?.CreateNestedContainer(context);

            try
            {
                var activity = nestedContainer
                               .With(context.Arguments)
                               .GetInstance <TActivity>();

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);
                activityContext.UpdatePayload(nestedContainer);

                return(new CreatedExecuteActivityScopeContext <IContainer, TActivity, TArguments>(nestedContainer, activityContext));
            }
            catch
            {
                nestedContainer.Dispose();
                throw;
            }
        }
コード例 #3
0
        public IExecuteActivityScopeContext <TActivity, TArguments> GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <ILifetimeScope>(out var existingLifetimeScope))
            {
                var activity = existingLifetimeScope.Resolve <TActivity>(TypedParameter.From(context.Arguments));

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext));
            }

            var parentLifetimeScope = _scopeProvider.GetLifetimeScope(context);

            var lifetimeScope = parentLifetimeScope.BeginLifetimeScope(_name, builder =>
            {
                builder.ConfigureScope(context);
                _configureScope?.Invoke(builder, context);
            });

            try
            {
                var activity = lifetimeScope.Resolve <TActivity>(TypedParameter.From(context.Arguments));

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                activityContext.UpdatePayload(lifetimeScope);

                return(new CreatedExecuteActivityScopeContext <ILifetimeScope, TActivity, TArguments>(lifetimeScope, activityContext));
            }
            catch
            {
                lifetimeScope.Dispose();
                throw;
            }
        }
コード例 #4
0
        public IExecuteActivityScopeContext <TActivity, TArguments> GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <IKernel>(out var kernel))
            {
                kernel.UpdateScope(context);

                var activity = kernel.Resolve <TActivity>(new Arguments().AddTyped(context.Arguments));

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext, ReleaseComponent));
            }

            var scope = _kernel.CreateNewOrUseExistingMessageScope(context);

            try
            {
                var activity = _kernel.Resolve <TActivity>(new Arguments().AddTyped(context.Arguments));

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);
                activityContext.UpdatePayload(_kernel);

                return(new CreatedExecuteActivityScopeContext <IDisposable, TActivity, TArguments>(scope, activityContext, ReleaseComponent));
            }
            catch
            {
                scope.Dispose();
                throw;
            }
        }
コード例 #5
0
        public async Task Execute(ExecuteContext <TArguments> context, IPipe <ExecuteActivityContext <TActivity, TArguments> > next)
        {
            TActivity activity = null;

            try
            {
                activity = _executeFactory(context.Arguments);

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                await next.Send(activityContext).ConfigureAwait(false);
            }
            finally
            {
                switch (activity)
                {
                case IAsyncDisposable asyncDisposable:
                    await asyncDisposable.DisposeAsync().ConfigureAwait(false);

                    break;

                case IDisposable disposable:
                    disposable.Dispose();
                    break;
                }
            }
        }
        public IExecuteActivityScopeContext <TActivity, TArguments> GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <IServiceScope>(out var existingServiceScope))
            {
                existingServiceScope.UpdateScope(context);

                var activity = existingServiceScope.ServiceProvider.GetService <TActivity>();
                if (activity == null)
                {
                    throw new ConsumerException($"Unable to resolve activity type '{TypeMetadataCache<TActivity>.ShortName}'.");
                }

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext));
            }

            if (!context.TryGetPayload(out IServiceProvider serviceProvider))
            {
                serviceProvider = _serviceProvider;
            }

            var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope();

            try
            {
                serviceScope.UpdateScope(context);

                var activity = serviceScope.ServiceProvider.GetService <TActivity>();
                if (activity == null)
                {
                    throw new ConsumerException($"Unable to resolve activity type '{TypeMetadataCache<TActivity>.ShortName}'.");
                }

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                activityContext.UpdatePayload(serviceScope);

                return(new CreatedExecuteActivityScopeContext <IServiceScope, TActivity, TArguments>(serviceScope, activityContext));
            }
            catch
            {
                serviceScope.Dispose();

                throw;
            }
        }
        public async ValueTask <IExecuteActivityScopeContext <TActivity, TArguments> > GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <IServiceScope>(out var existingServiceScope))
            {
                existingServiceScope.SetCurrentConsumeContext(context);

                var activity = existingServiceScope.ServiceProvider.GetService <TActivity>();
                if (activity == null)
                {
                    throw new ConsumerException($"Unable to resolve activity type '{TypeMetadataCache<TActivity>.ShortName}'.");
                }

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext));
            }

            if (!context.TryGetPayload(out IServiceProvider serviceProvider))
            {
                serviceProvider = _serviceProvider;
            }

            var serviceScope = serviceProvider.CreateScope();

            try
            {
                ExecuteContext <TArguments> scopeContext = new ExecuteContextScope <TArguments>(context, serviceScope, serviceScope.ServiceProvider);

                serviceScope.SetCurrentConsumeContext(scopeContext);

                var activity = serviceScope.ServiceProvider.GetService <TActivity>();
                if (activity == null)
                {
                    throw new ConsumerException($"Unable to resolve activity type '{TypeMetadataCache<TActivity>.ShortName}'.");
                }

                ExecuteActivityContext <TActivity, TArguments> activityContext = scopeContext.CreateActivityContext(activity);

                return(new CreatedExecuteActivityScopeContext <IServiceScope, TActivity, TArguments>(serviceScope, activityContext));
            }
            catch
            {
                if (serviceScope is IAsyncDisposable asyncDisposable)
                {
                    await asyncDisposable.DisposeAsync().ConfigureAwait(false);
                }
                else
                {
                    serviceScope.Dispose();
                }

                throw;
            }
        }
        public IExecuteActivityScopeContext <TActivity, TArguments> GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <Scope>(out var existingScope))
            {
                existingScope.UpdateScope(context);

                var activity = existingScope
                               .Container
                               .GetInstance <TActivity>();

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext));
            }

            var scope = AsyncScopedLifestyle.BeginScope(_container);

            try
            {
                scope.UpdateScope(context);

                var activity = scope
                               .Container
                               .GetInstance <TActivity>();

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                activityContext.UpdatePayload(scope);

                return(new CreatedExecuteActivityScopeContext <Scope, TActivity, TArguments>(scope, activityContext));
            }
            catch
            {
                scope.Dispose();

                throw;
            }
        }
        public async ValueTask <IExecuteActivityScopeContext <TActivity, TArguments> > GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <Scope>(out var existingScope))
            {
                existingScope.UpdateScope(context);

                var activity = existingScope
                               .Container
                               .GetInstance <TActivity>();

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext));
            }

            var scope = AsyncScopedLifestyle.BeginScope(_container);

            try
            {
                ExecuteContext <TArguments> scopeContext = new ExecuteContextScope <TArguments>(context, scope);

                scope.UpdateScope(scopeContext);

                var activity = scope.Container.GetInstance <TActivity>();

                ExecuteActivityContext <TActivity, TArguments> activityContext = scopeContext.CreateActivityContext(activity);

                return(new CreatedExecuteActivityScopeContext <Scope, TActivity, TArguments>(scope, activityContext));
            }
            catch
            {
                await scope.DisposeScopeAsync().ConfigureAwait(false);

                throw;
            }
        }