public static IRestOptionsBuilder WithBaseUrl(this IRestOptionsBuilder builder, string baseUrl) { builder.Services.AddOrReplace <HttpClientFactory>(ServiceLifetime.Scoped, sp => () => new HttpClient { BaseAddress = new Uri(baseUrl) }); return(builder); }
public static IRestOptionsBuilder <TContract> WithBaseUrl <TContract>(this IRestOptionsBuilder <TContract> builder, string baseUrl) where TContract : class { builder.Services.AddOrReplace <HttpClientFactory <TContract> >(ServiceLifetime.Scoped, sp => () => new HttpClient { BaseAddress = new Uri(baseUrl) }); return(builder); }
public static IRestOptionsBuilder AddRestProxies(this IRestOptionsBuilder options, Assembly proxyAssembly) { foreach (var item in proxyAssembly.GetCustomAttributes <RestProxyAttribute>()) { options.Services.AddScoped(item.ContractType, item.ProxyType); } return(options); }
public static IRestOptionsBuilder Contract <TContract>(this IRestOptionsBuilder builder, Action <IRestOptionsBuilder <TContract> > subBuilder) where TContract : class { var sub = new RestOptionsBuilder <TContract>(builder.Services); subBuilder(sub); return(builder); }
public static IRestOptionsBuilder AddJsonFormatter(this IRestOptionsBuilder builder, Action <JsonSerializerSettings> options) { builder.Services.AddOrReplace <IContentFormatter, JsonContentFormatter>(ServiceLifetime.Singleton, sp => { var settings = new JsonSerializerSettings(); options(settings); return(new JsonContentFormatter(settings)); }); return(builder); }
public static IRestOptionsBuilder WithHttpClient(this IRestOptionsBuilder builder, Action <IServiceProvider, HttpClient> clientFactory, Action <IHttpClientBuilder> httpClientBuilder = null) { var clientBuilder = builder.Services.AddHttpClient("restclient.default", clientFactory); httpClientBuilder?.Invoke(clientBuilder); builder.Services.AddOrReplace <HttpClientFactory>(ServiceLifetime.Transient, sp => () => sp.GetRequiredService <IHttpClientFactory>().CreateClient("restclient.default")); return(builder); }
public static IRestOptionsBuilder <TContract> WithHttpClient <TContract>(this IRestOptionsBuilder <TContract> builder, Action <IServiceProvider, HttpClient> clientFactory, Action <IHttpClientBuilder> httpClientBuilder = null) where TContract : class { var clientKey = $"restclient.{typeof(TContract).FullName}"; var clientBuilder = builder.Services.AddHttpClient(clientKey, clientFactory); httpClientBuilder?.Invoke(clientBuilder); builder.Services.AddOrReplace <HttpClientFactory <TContract> >(ServiceLifetime.Scoped, sp => () => sp.GetRequiredService <IHttpClientFactory>().CreateClient(clientKey)); return(builder); }
public static IRestOptionsBuilder AddProtobufFormatter(this IRestOptionsBuilder builder, Func <TypeModel> model = null, bool makeDefault = false) { builder.Services.AddOrReplace <IContentFormatter, ProtobufContentFormatter>(ServiceLifetime.Singleton, sp => { return(new ProtobufContentFormatter(model?.Invoke() ?? RuntimeTypeModel.Default)); }); if (makeDefault) { builder.Services.AddOrReplace <DefaultFormatterSelector>(ServiceLifetime.Scoped, sp => () => "application/x-protobuf"); } return(builder); }
public static IRestOptionsBuilder WithHttpClient(this IRestOptionsBuilder builder, Func <IServiceProvider, HttpClient> clientFactory) { builder.Services.AddOrReplace <HttpClientFactory>(ServiceLifetime.Scoped, sp => () => clientFactory(sp)); return(builder); }
public static IRestOptionsBuilder <TContract> WithHttpClient <TContract>(this IRestOptionsBuilder <TContract> builder, Func <IServiceProvider, HttpClient> clientFactory) where TContract : class { builder.Services.AddOrReplace <HttpClientFactory <TContract> >(ServiceLifetime.Scoped, sp => () => clientFactory(sp)); return(builder); }
public static IRestOptionsBuilder DefaultFormatter(this IRestOptionsBuilder builder, Func <IServiceProvider, string> mimeTypeSelector) { builder.Services.AddOrReplace <DefaultFormatterSelector>(ServiceLifetime.Scoped, sp => () => mimeTypeSelector(sp)); return(builder); }
public static IRestOptionsBuilder <TContract> WithBaseUrl <TContract>(this IRestOptionsBuilder <TContract> builder, string baseUrl, Action <IHttpClientBuilder> httpClientBuilder = null) where TContract : class { return(builder.WithHttpClient((sp, client) => client.BaseAddress = new Uri(baseUrl), httpClientBuilder)); }
public static IRestOptionsBuilder WithBaseUrl(this IRestOptionsBuilder builder, string baseUrl, Action <IHttpClientBuilder> httpClientBuilder = null) { return(builder.WithHttpClient((sp, client) => client.BaseAddress = new Uri(baseUrl), httpClientBuilder)); }