现在我遇到与this question相同的问题,因为在开始使用HttpClient后无法更改BaseAddress.
该问题的答案回答了一个无法完成的解释.您无法更改BaseAddress.
所以,如果我们想要更改ip地址以与其他服务器通信,我的问题(与链接的不同,所以不重复)是该怎么办?
我们应该实例化另一个HttpClient吗? (不,我们不打算使用)我们如何正确地从这里开始?
就目前而言,您无法更改基址.How do we correctly proceed from here?
不要设置基址,只需使用请求的完整地址.
这样,相同的客户端可以用于为每个基地址创建新客户端所需的所有其他请求,这也将破坏拥有单个客户端的优势.
asp.net core 2中的客户端工厂已经解决了与多个客户端相关的问题.
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.
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.
完成文件Use HttpClientFactory to implement resilient HTTP requests