Is IFlurlRequest
reentrant?
I am asking this question to understand if the following is a valid supported scenario.
We run an ASP.NET service, so it can execute multiple requests in parallel. And this service calls another http service using Flurl.Http nuget. Since the base URL is always the same, we would like to call new FlurlRequest(baseUrl).ConfigureRequest(...)
just once and store the result. And then we would use this precreated preconfigured IFlurlRequest
to execute multiple requests, even in parallel.
I think this approach smells as classes are not reentrant by default. But a colleague of mine tried it and says it works. So I would like to know if it is really safe to keep. Or if we should cache only configuration object and create a new FlurlRequest
for each http request.
No, IFlurlRequest is not inherently reentrant. Sharing a single instance of IFlurlRequest across multiple threads for simultaneous requests may lead to unpredictable behavior. The typical usage pattern of IFlurlRequest is for it to be created, optionally modified, and executed in a single chain. For example:
var response = await new FlurlRequest("https://api.example.com")
.WithHeader("Authorization", "Bearer token")
.PostJsonAsync(new { name = "example" });
After the request is executed, creating a new IFlurlRequest is often a better practice than reusing an existing one.