httpclient此实例已经启动了一个或多个请求。只能在发送第一个请求之前修改属性
我是在.NET Core 2.1中创建一个应用程序,并且正在使用HTTP客户端进行Web请求。问题是我必须发送并行呼叫以节省时间,为此我正在使用task.whenall()方法,但是当我点击此方法时,我会收到错误。“此实例已经启动了一个或多个请求。只能修改属性在发送第一个请求之前,“以前我正在使用RestSharp,一切都很好,但是我想使用HTTPCLEINT。这是代码:
public async Task<User> AddUser(string email)
{
var url = "user/";
_client.BaseAddress = new Uri("https://myWeb.com/");
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Constants."application/json"));
_client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
var json = new {email = email }
var response = await _client.PostAsJsonAsync(url,json);
if (response .IsSuccessStatusCode)
{ ....
这是构造函数:
private readonly HttpClient _httpClient;
public UserRepository(HttpClient httpClient)
{
_httpClient = httpClient;
}
方法调用:
var user1 = AddUser("[email protected]");
var user2 = AddUser("[email protected]");
await Task.WhenAll(user1, user2);
这是启动配置:
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
那我做错了什么?我需要使用addransient()更改addsingleton,还是还有其他问题。在响应后,我还需要使用_client.dispose(),因为我遵循的教程没有使用dispose方法,所以我对此并不感到困惑。
I am creating an application in .Net Core 2.1 and I am using http client for web requests. The issue is I have to send parallel calls to save time and for that I am using Task.WhenAll() method but when I hit this method I get the error "This instance has already started one or more requests. Properties can only be modified before sending the first request" Previously I was using RestSharp and everything was fine but I want to use httpclient. Here is the code:
public async Task<User> AddUser(string email)
{
var url = "user/";
_client.BaseAddress = new Uri("https://myWeb.com/");
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Constants."application/json"));
_client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
var json = new {email = email }
var response = await _client.PostAsJsonAsync(url,json);
if (response .IsSuccessStatusCode)
{ ....
Here is the constructor:
private readonly HttpClient _httpClient;
public UserRepository(HttpClient httpClient)
{
_httpClient = httpClient;
}
Method calling:
var user1 = AddUser("[email protected]");
var user2 = AddUser("[email protected]");
await Task.WhenAll(user1, user2);
and here is the startup configuation:
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
So what am I doing wrong? Do I need to change AddSingleton with AddTransient() or is there any other issue. One more question do I need to use _client.Dispose() after the response because the tutorial which I followed didn't use dispose method so I am little confused in that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
httpclient.defaultrequestheaders
(和baseaddress
)只能在提出任何请求之前设置一次。httpclient
如果您在使用后不修改它,则只能安全用作单顿。而不是设置
defaultrequestheaders
,而是在每个httprequestmessage上设置标头
您要发送的标题。用JSON替换
“ {...}”
。HttpClient.DefaultRequestHeaders
(andBaseAddress
) should only be set once, before you make any requests.HttpClient
is only safe to use as a singleton if you don't modify it once it's in use.Rather than setting
DefaultRequestHeaders
, set the headers on eachHttpRequestMessage
you are sending.Replace
"{...}"
with your JSON.也许我的两分钱会帮助某人。
调试应用程序时,我会在刷新页面时遇到此问题。
我当时正在使用单身人士,但是每个刷新都试图设置基础地址。因此,我只是将其包装在检查中,以查看是否已经设置了基本地址。
对我来说,即使已经设定了Baseaddress,它还是试图设定BaseadDress。您不能使用httpclient做到这一点。
Maybe my two cents will help someone.
I ran into this issue when refreshing the page when debugging the application.
I was using a singleton, but each refresh, it was trying to set the base address. So I just wrapped it in a check to see if the base address had already been set.
The issue for me was, it was trying to set the baseAddress, even though it was already set. You can't do this with a httpClient.
该问题是由与HTTPCLIENT相同实例重置baseaddress和标头引起的。
我尝试过
,但我不热衷于此。
我认为,更好的解决方案是使用HTTPCLIENTFACTORY。这将在使用后终止和垃圾收集HTTPClient的实例。
The issue is caused by resetting BaseAddress and headers for the same instance of the httpclient.
I tried
but I am not keen on this.
In my opinion, a better soloution is to use the httpclientFactory. This will terminate and garbage collect the instance of the httpclient after its use.
当您在消息中添加请求URL和标头时,它可以很好地工作,而不是在客户端上。因此,最好不要分配给
baseaddress
或headerdefaultrequestheaders
如果您将它们用于许多请求。It Works well when you add the request url and the headers at the message, rather than at the client. So better not to assign to
BaseAddress
Or the headerDefaultRequestHeaders
if you will use them for many requests.这个问题现在已经有时了,但是,我只是想到这个问题,试图在
Parallel.ForeachAsync
循环中调用客户端。根据答案的解决, Mali tbt
JUSTED
ihtttpclientfactory
在每次迭代中创建客户端实例。适用于OP案件:
This question has sometime now but, I just hit this issue trying to call a client within a
Parallel.ForEachAsync
loop.Solved based on the answer by Mali Tbt
Just injected
IHttpClientFactory
in the constructor, and used it to create a client instances on each iteration.Applying to OP case: