Beispiel #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GraphMiddlewareInvoker{TContext}" /> class.
 /// </summary>
 /// <param name="middlewareComponent">The middleware component.</param>
 /// <param name="next">The next.</param>
 public GraphMiddlewareInvoker(
     GraphMiddlewareDefinition <TContext> middlewareComponent,
     GraphMiddlewareInvocationDelegate <TContext> next = null)
 {
     this.ComponentDefinition = Validation.ThrowIfNullOrReturn(middlewareComponent, nameof(middlewareComponent));
     _singletonInstance       = this.ComponentDefinition.Component;
     this.Next = next;
     if (this.Next == null)
     {
         this.Next = (_, __) => Task.CompletedTask;
     }
 }
Beispiel #2
0
        public async Task InvokeAsync(TContext context, CancellationToken cancelToken)
        {
            // stop processing when cancelation is requested
            if (cancelToken.IsCancellationRequested)
            {
                return;
            }

            // create and use the component, depending on the scope
            // invoke it and return
            var instance = _singletonInstance;

            if (instance == null)
            {
                // no pre-instantiated component was provided
                // generate the compoennt from the current context. If it was marked as a singleton scope
                // store the reference for use on subsequent calls
                instance = context?.ServiceProvider?
                           .GetService(this.ComponentDefinition.MiddlewareType) as IGraphMiddlewareComponent <TContext>;

                if (this.ComponentDefinition.Lifetime == ServiceLifetime.Singleton)
                {
                    lock (_locker)
                    {
                        if (_singletonInstance == null)
                        {
                            _singletonInstance = instance;
                        }
                    }
                }
            }

            if (instance == null)
            {
                throw new GraphPipelineMiddlewareInvocationException(
                          this.ComponentDefinition.Name,
                          $"Unable to resolve an instance of the middleware compoent '{this.ComponentDefinition.Name}'. Either no instance was provided " +
                          "or one could not be created from the service provider on the request context.");
            }

            var task = instance.InvokeAsync(context, this.Next, cancelToken);
            await task.ConfigureAwait(false);
        }
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GraphMiddlewareDefinition{TMiddlewareComponent}" /> class.
 /// </summary>
 /// <param name="component">The component.</param>
 /// <param name="name">A friendly name to assign to this middleware component for easy reference.</param>
 public GraphMiddlewareDefinition(IGraphMiddlewareComponent <TContext> component, string name = null)
 {
     this.Component = Validation.ThrowIfNullOrReturn(component, nameof(component));
     this.Name      = name?.Trim() ?? component.GetType().FriendlyName();
     this.Lifetime  = ServiceLifetime.Singleton;
 }