public static IServiceCollection AddSimpleRpcClient( this IServiceCollection services, string clientName, HttpClientTransportOptions options, Action <IHttpClientBuilder> httpclientBuilder = null) { if (string.IsNullOrEmpty(clientName)) { throw new ArgumentNullException(nameof(clientName)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } var clientBuilder = services.AddHttpClient( clientName, client => { var url = new Uri(options.Url); client.BaseAddress = url; if (options.DefaultRequestHeaders != null) { foreach (var header in options.DefaultRequestHeaders) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } client.DefaultRequestHeaders.Add(Constants.Other.ApplicationName, options.ApplicationName); client.DefaultRequestHeaders.ConnectionClose = false; client.DefaultRequestHeaders.Host = url.Host; }); httpclientBuilder?.Invoke(clientBuilder); services.TryAddSingleton <IClientConfigurationManager, ClientConfigurationManager>(); services.TryAddSingleton <ISerializationHelper, SerializationHelper>(); services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IMessageSerializer), typeof(CerasMessageSerializer))); services.AddSingleton( sp => new ClientConfiguration { Name = clientName, Transport = new HttpClientTransport( clientName, sp.GetRequiredService <ISerializationHelper>().TryGetByTypeName(options.Serializer), sp.GetRequiredService <ISerializationHelper>(), sp.GetRequiredService <IHttpClientFactory>()) }); return(services); }
public static IServiceCollection AddSimpleRpcClient( this IServiceCollection services, string clientName, HttpClientTransportOptions options) { if (string.IsNullOrEmpty(clientName)) { throw new ArgumentNullException(nameof(clientName)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } services.AddHttpClient(clientName, (client) => { var url = new Uri(options.Url); client.BaseAddress = url; if (options.DefaultRequestHeaders != null) { foreach (var header in options.DefaultRequestHeaders) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } client.DefaultRequestHeaders.Add(Constants.Other.ApplicationName, options.ApplicationName); client.DefaultRequestHeaders.ConnectionClose = false; client.DefaultRequestHeaders.Host = url.Host; }); services.TryAddSingleton <IClientConfigurationManager, ClientConfigurationManager>(); services.AddSingleton(typeof(ClientConfiguration), (sp) => { var serializer = SerializationHelper.GetByName(options.Serializer); return(new ClientConfiguration() { Name = clientName, Transport = new HttpClientTransport(clientName, serializer, sp.GetRequiredService <IHttpClientFactory>()) }); }); return(services); }