更改HttpClient的基址 [英] Changing the Base Address of a HttpClient

查看:92
本文介绍了更改HttpClient的基址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写使用HttpClient的应用程序时,我的方式与这篇文章相同,换句话说,我不使用using ,而是使用静态HttpClient。当我只想与一台服务器通信时,我没有任何问题。 (我将IP地址设置为 BaseAddress 并继续)

When writing an application that uses HttpClient I have the same approach as this post, in other words I don't use using and instead use a static HttpClient. I have had no problems with that when I want to communicate with only one server. (I set the Ip address as BaseAddress and proceed)

现在,我与此问题关于开始使用HttpClient后无法更改BaseAddress的事实。

Now I have the same problem as this question regarding the fact that BaseAddress can not be changed after starting to use the HttpClient.

该问题的答案给出了无法完成的解释。您不能更改BaseAddress。

That question's answers respond with an explanation that it can not be done. You can not change BaseAddress.

因此,我的问题(与链接的问题不同,因此不是重复的问题)是我们应该怎么做。是否要更改IP地址以与其他服务器通信?

So my question (which is different than the linked one so not a duplicate) is what to do if we want to change the ip adress to communicate with other server?

我们应该实例化另一个HttpClient吗? (不,我们不会使用 using )如何正确地从这里开始?

Should we instantiate another HttpClient? (no, we are not going to use using) How do we correctly proceed from here?

推荐答案

按现状,您不能更改基址。

As it stands you can't change the base address.

我们如何正确地从这里开始?

How do we correctly proceed from here?

不要设置基地址,而只需使用请求的完整地址。

Do not set the base address and just use the full addresses for the requests.

这样,同一客户端可以用于所有请求,否则您将需要为每个基址创建一个新客户端,这也将使拥有单个客户端的优势失效客户。

That way the same client can be used for all requests other wise you would need to create a new client for each base address which will also defeat the advantages of having the single client.

asp.net core 2+中的客户端工厂此后已解决了与拥有多个客户端相关的问题。

The client factory in asp.net core 2+ has since fixed the problems associated with having multiple clients.


处置客户端不是强制性的,但这样做会取消所有正在进行的请求,并确保在调用Dispose之后不能使用给定的HttpClient实例。工厂负责跟踪和处置HttpClient实例使用的重要资源,这意味着HttpClient实例通常可以视为不需要处置的.NET对象。

Disposing of the client is not mandatory, but doing so will cancel any ongoing requests and ensure the given instance of HttpClient cannot be used after Dispose is called. The factory takes care of tracking and disposing of the important resources that instances of HttpClient use, which means that HttpClient instances can be generally be treated as .NET objects that don’t require disposing.

这样做的一个效果是,如今人们不再需要某些常见的模式来处理HttpClient实例,例如,使一个HttpClient实例长时间处于活动状态。关于工厂的确切功能以及解决的模式的文档将可用,但尚未完成。

One effect of this is that some common patterns that people use today to handle HttpClient instances, such as keeping a single HttpClient instance alive for a long time, are no longer required. Documentation about what exactly the factory does and what patterns it resolves will be available, but hasn’t been completed yet.

完整的文档< href = https://docs.microsoft.com/zh-cn/dotnet/standard/microservices-architecture/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests rel = noreferrer >使用HttpClientFactory实施弹性HTTP请求

这篇关于更改HttpClient的基址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆