Refactor HTTP requests to enable parallel execution
Replaced sequential `foreach` loop with LINQ's `Select` to create a collection of tasks for sending HTTP requests asynchronously. Added `Task.WhenAll` to execute all requests concurrently, improving performance. Removed the old `foreach` implementation in favor of a functional programming approach.
This commit is contained in:
parent
1e35e4a057
commit
d80e0d3562
@ -21,7 +21,7 @@ public class InvokeRecActionCommandHandler(ISender sender, IHttpClientFactory cl
|
|||||||
|
|
||||||
var http = clientFactory.CreateClient();
|
var http = clientFactory.CreateClient();
|
||||||
|
|
||||||
foreach (var action in actions)
|
var tasks = actions.Select(async action =>
|
||||||
{
|
{
|
||||||
var method = new HttpMethod(action.RestType.ToUpper());
|
var method = new HttpMethod(action.RestType.ToUpper());
|
||||||
var msg = new HttpRequestMessage(method, action.EndpointUri);
|
var msg = new HttpRequestMessage(method, action.EndpointUri);
|
||||||
@ -29,6 +29,8 @@ public class InvokeRecActionCommandHandler(ISender sender, IHttpClientFactory cl
|
|||||||
var response = await http.SendAsync(msg, cancel);
|
var response = await http.SendAsync(msg, cancel);
|
||||||
var body = await response.Content.ReadAsStringAsync(cancel);
|
var body = await response.Content.ReadAsStringAsync(cancel);
|
||||||
var headers = response.Headers.ToDictionary();
|
var headers = response.Headers.ToDictionary();
|
||||||
}
|
});
|
||||||
|
|
||||||
|
await Task.WhenAll(tasks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user