Esempio n. 1
0
        private CallInvoker CreateInvoker(EntryKey key)
        {
            var(name, type) = (key.Name, key.Type);
            var scope    = _scopeFactory.CreateScope();
            var services = scope.ServiceProvider;

            try
            {
                var httpClientFactoryOptions = _httpClientFactoryOptionsMonitor.Get(name);
                if (httpClientFactoryOptions.HttpClientActions.Count > 0)
                {
                    throw new InvalidOperationException($"The ConfigureHttpClient method is not supported when creating gRPC clients. Unable to create client with name '{name}'.");
                }

                var clientFactoryOptions = _grpcClientFactoryOptionsMonitor.Get(name);
                var httpHandler          = _messageHandlerFactory.CreateHandler(name);
                if (httpHandler == null)
                {
                    throw new ArgumentNullException(nameof(httpHandler));
                }

                var channelOptions = new GrpcChannelOptions();
                channelOptions.HttpHandler     = httpHandler;
                channelOptions.LoggerFactory   = _loggerFactory;
                channelOptions.ServiceProvider = services;

                if (clientFactoryOptions.ChannelOptionsActions.Count > 0)
                {
                    foreach (var applyOptions in clientFactoryOptions.ChannelOptionsActions)
                    {
                        applyOptions(channelOptions);
                    }
                }

                var address = clientFactoryOptions.Address;
                if (address == null)
                {
                    throw new InvalidOperationException($@"Could not resolve the address for gRPC client '{name}'. Set an address when registering the client: services.AddGrpcClient<{type.Name}>(o => o.Address = new Uri(""https://localhost:5001""))");
                }

                var channel = GrpcChannel.ForAddress(address, channelOptions);

                var httpClientCallInvoker = channel.CreateCallInvoker();

                var resolvedCallInvoker = GrpcClientFactoryOptions.BuildInterceptors(
                    httpClientCallInvoker,
                    services,
                    clientFactoryOptions,
                    InterceptorScope.Channel);

                return(resolvedCallInvoker);
            }
            catch
            {
                // If something fails while creating the handler, dispose the services.
                scope?.Dispose();
                throw;
            }
        }
Esempio n. 2
0
 protected override void Options(GrpcClientFactoryOptions options)
 {
     options.Address = new Uri($"https://{Target.GRPC_CHANNEL_IP}:{Target.GRPC_CHANNEL_PORT}");
     options.ChannelOptionsActions.Add(chOptions => {
         // Return "true" to allow certificates that are untrusted/invalid
         chOptions.HttpHandler = new HttpClientHandler {
             ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
         };
     });
 }
        public override TClient CreateClient <
#if NET5_0_OR_GREATER
            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
            TClient>(string name) where TClient : class
        {
            var defaultClientActivator = _serviceProvider.GetService <DefaultClientActivator <TClient> >();

            if (defaultClientActivator == null)
            {
                throw new InvalidOperationException($"No gRPC client configured with name '{name}'.");
            }

            var callInvoker = _callInvokerFactory.CreateInvoker(name, typeof(TClient));

            var clientFactoryOptions = _grpcClientFactoryOptionsMonitor.Get(name);

            var resolvedCallInvoker = GrpcClientFactoryOptions.BuildInterceptors(
                callInvoker,
                _serviceProvider,
                clientFactoryOptions,
                InterceptorScope.Client);

#pragma warning disable CS0618 // Type or member is obsolete
            if (clientFactoryOptions.Interceptors.Count != 0)
            {
                resolvedCallInvoker = resolvedCallInvoker.Intercept(clientFactoryOptions.Interceptors.ToArray());
            }
#pragma warning restore CS0618 // Type or member is obsolete

            if (clientFactoryOptions.CallOptionsActions.Count != 0)
            {
                resolvedCallInvoker = new CallOptionsConfigurationInvoker(resolvedCallInvoker, clientFactoryOptions.CallOptionsActions, _serviceProvider);
            }

            if (clientFactoryOptions.Creator != null)
            {
                var c = clientFactoryOptions.Creator(resolvedCallInvoker);
                if (c is TClient client)
                {
                    return(client);
                }
                else if (c == null)
                {
                    throw new InvalidOperationException("A null instance was returned by the configured client creator.");
                }

                throw new InvalidOperationException($"The {c.GetType().FullName} instance returned by the configured client creator is not compatible with {typeof(TClient).FullName}.");
            }
            else
            {
                return(defaultClientActivator.CreateClient(resolvedCallInvoker));
            }
        }
Esempio n. 4
0
        public void Configure(string name, GrpcClientFactoryOptions client)
        {
            var options = _options.Get(name);

            if (options.Address != null)
            {
                var address = new Regex(@"^(?!\w+:\/\/)")
                              .Replace(options.Address, "http://");
                client.Address = new Uri(address);
            }

            client.Interceptors.AddCorrelationId(_services);

            if (options.RetryCount != null)
            {
                client.Interceptors.AddExponentialBackoff(_services,
                                                          options.RetryCount.Value, options.RetryInterval, options.RetryForever);
            }
        }
        public CallInvoker CreateCallInvoker(HttpClient httpClient, string name, GrpcClientFactoryOptions clientFactoryOptions)
        {
            if (httpClient == null)
            {
                throw new ArgumentNullException(nameof(httpClient));
            }

            var channelOptions = new GrpcChannelOptions();

            channelOptions.HttpClient    = httpClient;
            channelOptions.LoggerFactory = _loggerFactory;

            if (clientFactoryOptions.ChannelOptionsActions.Count > 0)
            {
                foreach (var applyOptions in clientFactoryOptions.ChannelOptionsActions)
                {
                    applyOptions(channelOptions);
                }
            }

            var address = clientFactoryOptions.Address ?? httpClient.BaseAddress;

            if (address == null)
            {
                throw new InvalidOperationException($"Could not resolve the address for gRPC client '{name}'.");
            }

            var channel = GrpcChannel.ForAddress(address, channelOptions);

            var httpClientCallInvoker = channel.CreateCallInvoker();

            var resolvedCallInvoker = clientFactoryOptions.Interceptors.Count == 0
                ? httpClientCallInvoker
                : httpClientCallInvoker.Intercept(clientFactoryOptions.Interceptors.ToArray());

            return(resolvedCallInvoker);
        }
        private CallInvoker CreateInvoker(EntryKey key)
        {
            var(name, type) = (key.Name, key.Type);
            var scope    = _scopeFactory.CreateScope();
            var services = scope.ServiceProvider;

            try
            {
                var httpClientFactoryOptions = _httpClientFactoryOptionsMonitor.Get(name);
                if (httpClientFactoryOptions.HttpClientActions.Count > 0)
                {
                    throw new InvalidOperationException($"The ConfigureHttpClient method is not supported when creating gRPC clients. Unable to create client with name '{name}'.");
                }

                var clientFactoryOptions = _grpcClientFactoryOptionsMonitor.Get(name);
                var httpHandler          = _messageHandlerFactory.CreateHandler(name);
                if (httpHandler == null)
                {
                    throw new ArgumentNullException(nameof(httpHandler));
                }

                var channelOptions = new GrpcChannelOptions();
                channelOptions.HttpHandler     = httpHandler;
                channelOptions.LoggerFactory   = _loggerFactory;
                channelOptions.ServiceProvider = services;

                if (clientFactoryOptions.ChannelOptionsActions.Count > 0)
                {
                    foreach (var applyOptions in clientFactoryOptions.ChannelOptionsActions)
                    {
                        applyOptions(channelOptions);
                    }
                }

                var address = clientFactoryOptions.Address;
                if (address == null)
                {
                    throw new InvalidOperationException($@"Could not resolve the address for gRPC client '{name}'. Set an address when registering the client: services.AddGrpcClient<{type.Name}>(o => o.Address = new Uri(""https://*****:*****@"client.AddCallCredentials((context, metadata) => {}).ConfigureChannel(o => o.UnsafeUseInsecureChannelCallCredentials = true)");
                }

                var channel = GrpcChannel.ForAddress(address, channelOptions);

                var httpClientCallInvoker = channel.CreateCallInvoker();

                var resolvedCallInvoker = GrpcClientFactoryOptions.BuildInterceptors(
                    httpClientCallInvoker,
                    services,
                    clientFactoryOptions,
                    InterceptorScope.Channel);

                return(resolvedCallInvoker);
            }
            catch
            {
                // If something fails while creating the handler, dispose the services.
                scope?.Dispose();
                throw;
            }
        }
Esempio n. 7
0
 protected abstract void Options(GrpcClientFactoryOptions options);
        public CallInvoker CreateCallInvoker(HttpMessageHandler httpHandler, string name, Type type, GrpcClientFactoryOptions clientFactoryOptions)
        {
            if (httpHandler == null)
            {
                throw new ArgumentNullException(nameof(httpHandler));
            }

            var channelOptions = new GrpcChannelOptions();

            channelOptions.HttpHandler   = httpHandler;
            channelOptions.LoggerFactory = _loggerFactory;

            if (clientFactoryOptions.ChannelOptionsActions.Count > 0)
            {
                foreach (var applyOptions in clientFactoryOptions.ChannelOptionsActions)
                {
                    applyOptions(channelOptions);
                }
            }

            var address = clientFactoryOptions.Address;

            if (address == null)
            {
                throw new InvalidOperationException($@"Could not resolve the address for gRPC client '{name}'. Set an address when registering the client: services.AddGrpcClient<{type.Name}>(o => o.Address = new Uri(""https://localhost:5001""))");
            }

            var channel = GrpcChannel.ForAddress(address, channelOptions);

            var httpClientCallInvoker = channel.CreateCallInvoker();

            var resolvedCallInvoker = clientFactoryOptions.Interceptors.Count == 0
                ? httpClientCallInvoker
                : httpClientCallInvoker.Intercept(clientFactoryOptions.Interceptors.ToArray());

            return(resolvedCallInvoker);
        }
 protected virtual void ConfigureGrpcClient(GrpcClientFactoryOptions options)
 {
 }