简体   繁体   中英

Do I need to create a new HttpClient for subsequent requests to new BaseAddress?

Take for example the following, I use a single httpClient, assign the BaseAddress, and then pass it to the client that will ultimately use it:

    var httpClient = httpClientFactory.CreateClient("morningstar-api");

    httpClient.BaseAddress = new Uri("http://UniqueAddress_1");
    _mfEODListApi = new RestEase.RestClient(httpClient);

    httpClient.BaseAddress = new Uri("http://UniqueAddress_2");
    _MFListApi = new RestEase.RestClient(httpClient)();

Is it better to assign the different base address as specified above, or should I create a new httpClient for each and give each new httpClient its unique BaseAddress?

Following the pattern above, I subsequently caught an exception:

Unexpected character encountered while parsing value: <. Path '', line 0, position 0.

But I don't know if this unfortunately meaningless message is due to the way I'm configuring the httpClient or something else totally unrelated.

I'd like to have a better idea of the best practice for creating httpClients for calls to different BaseAddresses.

Depending on your use case, the MS Documentation gives you some ideas for which pattern to use.

There are several ways IHttpClientFactory can be used in an app:

Basic usage

Named clients

Typed clients

Generated clients

The best approach depends upon the app's requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM