如何将 NamedHttpClient 注入 TypedHttpClient
How to inject a NamedHttpClient to TypedHttpClient
在我的 .Net 核心应用程序中,我有 NamedHttpClient 和 TypedHttpClient。我需要使用 NamedClient 作为 TypedHttpClient 中的默认 httpclient。
我的配置服务:
public static IServiceCollection ConfigureServiceOptions(this IServiceCollection services, IConfiguration config)
{
IConfigurationSection serverSection = config.GetSection(nameof(ServerOptions));
services.Configure<ServerOptions>(serverSection);
//Named
services.AddHttpClient("defaultHttpClient", client =>
{
client.BaseAddress = new System.Uri(dataServerSection.Get<ServerOptions>().ServerUrl);
client.DefaultRequestHeaders.Add("Accept", "application/xml, */*");
client.DefaultRequestHeaders.Add("User-Agent", "Server v1.0.0");
});
}
//startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureServiceOptions(Configuration);
//Adding Typed
services.AddHttpClient<DataService>(); //I need to use defaultHttpClient as HttpClient Inside DataService
}
到目前为止我找到了3种方法,不知道有没有什么方法有问题
方式一:
services.AddHttpClient<DataService>();
public class DataService
{
private HttpClient Client { get;}
public DataService(/*need to pass this otherwise cant resolve*/ HttpClient client, IHttpClientFactory factory)
{
Client = factory.CreateClient("defaultHttpClient");
}
}
在方法 1 中,我需要在构造函数中添加额外的 HttpClient 客户端参数。否则数据服务未解析。
方式二:
services.AddTransient<DataService>( cfg =>
{
var clientFactory = cfg.GetRequiredService<System.Net.Http.IHttpClientFactory>();
var httpClient = clientFactory.CreateClient("defaultHttpClient");
return new DataService(httpClient);
});
public class DataService
{
private HttpClient Client { get;}
public DataService(HttpClient client)
{
Client = client;
}
}
方式三:
services.AddTransient<DataService>();
public class DataService
{
private HttpClient Client { get;}
public DataService(IHttpClientFactory factory)
{
Client = factory.CreateClient("defaultHttpClient");
}
}
我认为 Way2 和 Way3 可能是一样的。不知道有没有区别。
谁能告诉我推荐的方法是什么?或者还有其他方法吗?
由于您实际上并未为 DataService
设置类型化客户端,因此“方式 1”没有多大意义,它基本上是“方式 3”,但有额外的步骤。
“方式 2”使服务代码更简洁,但如果以后需要新的依赖项,维护起来会很麻烦。
“方式 3”是如何使用命名 HttpClient
在 docs 中显示的,所以我会使用它。
在我的 .Net 核心应用程序中,我有 NamedHttpClient 和 TypedHttpClient。我需要使用 NamedClient 作为 TypedHttpClient 中的默认 httpclient。
我的配置服务:
public static IServiceCollection ConfigureServiceOptions(this IServiceCollection services, IConfiguration config)
{
IConfigurationSection serverSection = config.GetSection(nameof(ServerOptions));
services.Configure<ServerOptions>(serverSection);
//Named
services.AddHttpClient("defaultHttpClient", client =>
{
client.BaseAddress = new System.Uri(dataServerSection.Get<ServerOptions>().ServerUrl);
client.DefaultRequestHeaders.Add("Accept", "application/xml, */*");
client.DefaultRequestHeaders.Add("User-Agent", "Server v1.0.0");
});
}
//startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureServiceOptions(Configuration);
//Adding Typed
services.AddHttpClient<DataService>(); //I need to use defaultHttpClient as HttpClient Inside DataService
}
到目前为止我找到了3种方法,不知道有没有什么方法有问题
方式一:
services.AddHttpClient<DataService>();
public class DataService
{
private HttpClient Client { get;}
public DataService(/*need to pass this otherwise cant resolve*/ HttpClient client, IHttpClientFactory factory)
{
Client = factory.CreateClient("defaultHttpClient");
}
}
在方法 1 中,我需要在构造函数中添加额外的 HttpClient 客户端参数。否则数据服务未解析。
方式二:
services.AddTransient<DataService>( cfg =>
{
var clientFactory = cfg.GetRequiredService<System.Net.Http.IHttpClientFactory>();
var httpClient = clientFactory.CreateClient("defaultHttpClient");
return new DataService(httpClient);
});
public class DataService
{
private HttpClient Client { get;}
public DataService(HttpClient client)
{
Client = client;
}
}
方式三:
services.AddTransient<DataService>();
public class DataService
{
private HttpClient Client { get;}
public DataService(IHttpClientFactory factory)
{
Client = factory.CreateClient("defaultHttpClient");
}
}
我认为 Way2 和 Way3 可能是一样的。不知道有没有区别。
谁能告诉我推荐的方法是什么?或者还有其他方法吗?
由于您实际上并未为 DataService
设置类型化客户端,因此“方式 1”没有多大意义,它基本上是“方式 3”,但有额外的步骤。
“方式 2”使服务代码更简洁,但如果以后需要新的依赖项,维护起来会很麻烦。
“方式 3”是如何使用命名 HttpClient
在 docs 中显示的,所以我会使用它。