예제 #1
0
        public OAuthAuthenticated CreateOAuthTokenAuthenticated()
        {
            HttpClient client = HttpClient;

            if (client == null)
            {
                if (HttpClientFactory != null)
                {
                    client = HttpClientFactory.CreateClient();
                }
                else if (HttpMessageHandler != null)
                {
                    client = new HttpClient(HttpMessageHandler);
                }
                else if (HttpMessageHandlerFactory != null)
                {
                    client = new HttpClient(HttpMessageHandlerFactory.CreateHandler());
                }
                else
                {
                    client = new HttpClient();
                }
            }

            return(new OAuthAuthenticated(client, OAuthToken, ConfiguredScopes));
        }
예제 #2
0
        public override TClient CreateClient <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 clientFactoryOptions = _clientFactoryOptionsMonitor.Get(name);
            var httpHandler          = _messageHandlerFactory.CreateHandler(name);
            var callInvoker          = _callInvokerFactory.CreateCallInvoker(httpHandler, name, typeof(TClient), clientFactoryOptions);

            if (clientFactoryOptions.Creator != null)
            {
                var c = clientFactoryOptions.Creator(callInvoker);
                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(callInvoker));
            }
        }
 public HttpMessageHandlerBehavior(IHttpMessageHandlerFactory factory, string serviceName)
 {
     // Here we prescribe how handler will be created.
     // Since it uses IHttpMessageHandlerFactory, this factory will manage the setup and lifetime of the handler,
     // based on the configuration we provided with AddHttpClient(serviceName)
     _httpMessageHandler = () => factory.CreateHandler(serviceName);
 }
예제 #4
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;
            }
        }
예제 #5
0
        public HttpClient CreateClient(HttpClientOptions options)
        {
            Check.NotNull(options, nameof(options));
            var handler = _httpMessageHandlerFactory.CreateHandler(options);
            var client  = new HttpClient(handler, disposeHandler: false);

            return(client);
        }
        /// <summary>
        /// Creates a new <see cref="HttpMessageHandler"/> using the default configuration.
        /// </summary>
        /// <param name="factory">The <see cref="IHttpMessageHandlerFactory"/>.</param>
        /// <returns>An <see cref="HttpMessageHandler"/> configured using the default configuration.</returns>
        public static HttpMessageHandler CreateHandler(this IHttpMessageHandlerFactory factory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            return(factory.CreateHandler(Options.DefaultName));
        }
            public HttpClient CreateClient(string name)
            {
                var handler = _handlerFactory.CreateHandler(name);

                return(new HttpClient(handler));
            }
예제 #8
0
 HttpMessageHandler IHostingContext.CreateHttpHandler()
 => httpHandlerFactory?.CreateHandler(clientHandlerName) ?? new HttpClientHandler();
예제 #9
0
 internal AuthenticationHttpClient(IHttpMessageHandlerFactory httpMessageHandlerFactory, string name)
     : this(new AuthenticationHttpMessageHandler(httpMessageHandlerFactory.CreateHandler(name)))
 {
 }
예제 #10
0
 public AuthenticationHttpClient(IHttpMessageHandlerFactory httpMessageHandlerFactory)
     : this(new AuthenticationHttpMessageHandler(httpMessageHandlerFactory.CreateHandler()))
 {
 }
 public CustomEndpointBehavior(IHttpMessageHandlerFactory factory)
 {
     factory.CreateHandler();
     _httpHandler = () => factory.CreateHandler();
 }
예제 #12
0
 internal OAuthAuthenticated(IHttpMessageHandlerFactory httpMessageHandlerFactory, string oauthToken, HashSet <TwitchConstants.TwitchOAuthScopes> scopes)
     : this(new HttpClient(httpMessageHandlerFactory.CreateHandler()), oauthToken, scopes)
 {
 }
        /// <summary>
        /// Creates a new <see cref="HttpMessageHandler"/> using the default configuration.
        /// </summary>
        /// <param name="factory">The <see cref="IHttpMessageHandlerFactory"/>.</param>
        /// <returns>An <see cref="HttpMessageHandler"/> configured using the default configuration.</returns>
        public static HttpMessageHandler CreateHandler(this IHttpMessageHandlerFactory factory)
        {
            ThrowHelper.ThrowIfNull(factory);

            return(factory.CreateHandler(Options.DefaultName));
        }
        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;
            }
        }
예제 #15
0
 internal ClientCredentialsAuthenticated(IHttpMessageHandlerFactory httpMessageHandlerFactory, string clientId, string clientSecret, HashSet <TwitchConstants.TwitchOAuthScopes> scopes)
     : this(new HttpClient(httpMessageHandlerFactory.CreateHandler()), clientId, clientSecret, scopes)
 {
 }