与IHttpClientFactory的异步BaseAddress解析

时间:2019-11-12 17:38:49

标签: .net asp.net-core dotnet-httpclient

在某个时候,我们编写了自己的HttpClientFactory以防止HttpClient套接字泄漏(https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/)(是的,我们很难学到。)

由于HttpClientFactory现在在.NET Core中可用,因此我们希望使用它,因为它是主动维护的,并且可能比我们的测试得更好。

问题在于,其他服务的基地址来自Consul(服务发现),而找到地址是异步操作。 HttpClientFactory的API没有异步回调,例如:

services.AddHttpClient("SomeService", x => { x.BaseAddress = new Uri(SomeService.ApiBaseUrl); })

而实际上我需要的是这样的东西:

services.AddHttpClient("SomeService", async x => { x.BaseAddress = await ...; })

我想这比看起来要难得多,因为这意味着它需要IoC容器/服务提供程序以允许异步解析,或者注册Task<SomeHttpClient>而不是SomeHttpClient

我的问题是,如何以动态/异步HttpClientFactory分辨率来BaseAddress

0 个答案:

没有答案