Пример #1
0
        internal static CallInvoker BuildInterceptors(
            CallInvoker callInvoker,
            IServiceProvider serviceProvider,
            GrpcClientFactoryOptions clientFactoryOptions,
            InterceptorScope scope)
        {
            CallInvoker resolvedCallInvoker;

            if (clientFactoryOptions.InterceptorRegistrations.Count == 0)
            {
                resolvedCallInvoker = callInvoker;
            }
            else
            {
                List <Interceptor>?channelInterceptors = null;
                for (var i = 0; i < clientFactoryOptions.InterceptorRegistrations.Count; i++)
                {
                    var registration = clientFactoryOptions.InterceptorRegistrations[i];
                    if (registration.Scope == scope)
                    {
                        channelInterceptors ??= new List <Interceptor>();
                        channelInterceptors.Add(registration.Creator(serviceProvider));
                    }
                }

                resolvedCallInvoker = channelInterceptors != null
                    ? callInvoker.Intercept(channelInterceptors.ToArray())
                    : callInvoker;
            }

            return(resolvedCallInvoker);
        }
        public async Task AddInterceptor_InterceptorLifetime_InterceptorCreatedCountCorrect(InterceptorScope scope, int callCount)
        {
            // Arrange
            var testHttpMessageHandler = new TestHttpMessageHandler();

            var interceptorCreatedCount = 0;
            var services = new ServiceCollection();

            services.AddTransient <CallbackInterceptor>(s =>
            {
                interceptorCreatedCount++;
                return(new CallbackInterceptor(o => { }));
            });

            services
            .AddGrpcClient <Greeter.GreeterClient>(o =>
            {
                o.Address = new Uri("http://localhost");
            })
            .AddInterceptor <CallbackInterceptor>(scope)
            .ConfigurePrimaryHttpMessageHandler(() => testHttpMessageHandler);

            var serviceProvider = services.BuildServiceProvider(validateScopes: true);

            // Act
            var clientFactory = serviceProvider.GetRequiredService <GrpcClientFactory>();

            var client1 = clientFactory.CreateClient <Greeter.GreeterClient>(nameof(Greeter.GreeterClient));
            await client1.SayHelloAsync(new HelloRequest()).ResponseAsync.DefaultTimeout();

            var client2 = clientFactory.CreateClient <Greeter.GreeterClient>(nameof(Greeter.GreeterClient));
            await client2.SayHelloAsync(new HelloRequest()).ResponseAsync.DefaultTimeout();

            // Assert
            Assert.AreEqual(callCount, interceptorCreatedCount);
        }
        /// <summary>
        /// Adds an additional interceptor from the dependency injection container for a gRPC client.
        /// </summary>
        /// <typeparam name="TInterceptor">The type of the <see cref="Interceptor"/>.</typeparam>
        /// <param name="scope">The scope of the interceptor.</param>
        /// <param name="builder">The <see cref="IHttpClientBuilder"/>.</param>
        /// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns>
        public static IHttpClientBuilder AddInterceptor <TInterceptor>(this IHttpClientBuilder builder, InterceptorScope scope)
            where TInterceptor : Interceptor
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            ValidateGrpcClient(builder);

            builder.AddInterceptor(scope, serviceProvider =>
            {
                return(serviceProvider.GetRequiredService <TInterceptor>());
            });

            return(builder);
        }
        /// <summary>
        /// Adds a delegate that will be used to create an additional inteceptor for a gRPC client.
        /// </summary>
        /// <param name="builder">The <see cref="IHttpClientBuilder"/>.</param>
        /// <param name="scope">The scope of the interceptor.</param>
        /// <param name="configureInvoker">A delegate that is used to create an <see cref="Interceptor"/>.</param>
        /// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns>
        public static IHttpClientBuilder AddInterceptor(this IHttpClientBuilder builder, InterceptorScope scope, Func <Interceptor> configureInvoker)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (configureInvoker == null)
            {
                throw new ArgumentNullException(nameof(configureInvoker));
            }

            ValidateGrpcClient(builder);

            builder.Services.Configure <GrpcClientFactoryOptions>(builder.Name, options =>
            {
                options.InterceptorRegistrations.Add(new InterceptorRegistration(scope, s => configureInvoker()));
            });

            return(builder);
        }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="InterceptorRegistration"/> class.
 /// </summary>
 /// <param name="scope">The scope of the interceptor.</param>
 /// <param name="creator">A delegate that is used to create an <see cref="Interceptor"/>.</param>
 public InterceptorRegistration(InterceptorScope scope, Func <IServiceProvider, Interceptor> creator)
 {
     Scope   = scope;
     Creator = creator;
 }