コード例 #1
0
        /// <summary>
        /// Get an invoker for an activator's pipeline.
        /// </summary>
        /// <param name="activator">The activator.</param>
        /// <param name="registry">The applicable component registry.</param>
        /// <returns>A func to call that invokes the pipeline.</returns>
        public static Func <ILifetimeScope, IEnumerable <Parameter>, T> GetPipelineInvoker <T>(this IInstanceActivator activator, IComponentRegistry registry)
        {
            var services        = new RegistryServices(registry);
            var pipelineBuilder = new ResolvePipelineBuilder(PipelineType.Registration);

            activator.ConfigurePipeline(services, pipelineBuilder);

            var built = pipelineBuilder.Build();

            return((scope, parameters) =>
            {
                // To get the sharing scope from what might be a container, we're going to resolve the lifetime scope.
                var lifetimeScope = scope.Resolve <ILifetimeScope>() as LifetimeScope;

                var request = new DefaultResolveRequestContext(
                    new ResolveOperation(lifetimeScope, lifetimeScope.DiagnosticSource),
                    new ResolveRequest(new TypedService(typeof(T)), Mocks.GetResolvableImplementation(), parameters),
                    lifetimeScope,
                    lifetimeScope.DiagnosticSource);

                built.Invoke(request);

                return (T)request.Instance;
            });
        }
コード例 #2
0
ファイル: ResolveOperation.cs プロジェクト: yus1977/Autofac
        private void InvokePipeline(ResolveRequest request, DefaultResolveRequestContext requestContext)
        {
            request.ResolvePipeline.Invoke(requestContext);
            if (requestContext.Instance == null)
            {
                throw new DependencyResolutionException(ResolveOperationResources.PipelineCompletedWithNoInstance);
            }

            _successfulRequests.Add(requestContext);
        }
コード例 #3
0
ファイル: ResolveOperation.cs プロジェクト: yus1977/Autofac
        /// <inheritdoc />
        public object GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, ResolveRequest request)
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (_ended)
            {
                throw new ObjectDisposedException(ResolveOperationResources.TemporaryContextDisposed, innerException: null);
            }

            // Create a new request context.
            var requestContext = new DefaultResolveRequestContext(this, request, currentOperationScope, DiagnosticSource);

            // Raise our request-beginning event.
            var handler = ResolveRequestBeginning;

            handler?.Invoke(this, new ResolveRequestBeginningEventArgs(requestContext));

            RequestDepth++;

            // Track the last active request and scope in the call stack.
            ResolveRequestContext?lastActiveRequest = ActiveRequestContext;
            var lastScope = CurrentScope;

            ActiveRequestContext = requestContext;
            CurrentScope         = currentOperationScope;

            try
            {
                // Same basic flow in if/else, but doing a one-time check for diagnostics
                // and choosing the "diagnostics enabled" version vs. the more common
                // "no diagnostics enabled" path: hot-path optimization.
                if (DiagnosticSource.IsEnabled())
                {
                    DiagnosticSource.RequestStart(this, requestContext);
                    InvokePipeline(request, requestContext);
                    DiagnosticSource.RequestSuccess(this, requestContext);
                }
                else
                {
                    InvokePipeline(request, requestContext);
                }
            }
            catch (Exception ex)
            {
                if (DiagnosticSource.IsEnabled())
                {
                    DiagnosticSource.RequestFailure(this, requestContext, ex);
                }

                throw;
            }
            finally
            {
                ActiveRequestContext = lastActiveRequest;
                CurrentScope         = lastScope;

                // Raise the appropriate completion events.
                if (RequestStack.Count == 0)
                {
                    CompleteRequests();
                }

                RequestDepth--;
            }

            // InvokePipeline throws if the instance is null but
            // analyzers don't pick that up and get mad.
            return(requestContext.Instance !);
        }