In FlUrl version 3.X, I have been working with my REST Api in this way:
- I have the singleton instance
_noAuthClient. With this client, I make a Login call to Rest Api, which returns JWT token. - Then I am creating another singleton instance
_authClientwith the call below and all subsequent calls are done through this_authClient. This ensures compatibility with AspNet.Core authorization.
private IFlurlClient CreateAuthClient()
{
lock (_authLock)
{
if (AuthToken == null)
{
throw new InvalidOperationException("AuthenticateAsync was not called. Client can be accessed after AuthenticateAsync() call.");
}
return _noAuthClient.WithOAuthBearerToken(AuthToken.AuthToken);
}
}
I think that in 4.0 this approach with singleton clients is already deprecated and I would like to use a more current approach. Could you give me some direction?
I am implementing 4.0 Flurl with Dependency container. Registering IFlurlClientCache as a singleton, configuring by .WithDefaults(builder => ...).
I have found this interesting solution, but this is also not aligned with 4.0.
For using DI and needing precise control of your client instances, I recommend using named clients, an idea that was...uh...borrowed from .NET's
IHttpClientFactory. Although you can pre-configure your clients during DI registration, I think you'll find thatGetOrAddis the way to go here, since you won't have that auth token at startup (I'm assuming).One thing to look out for is that in your code sample,
_noAuthClient.WithOAuthBearerTokendoesn't return a new client - it modifies the original. To have 2 instances, they each need to be created/configured from scratch.But that doesn't mean you can't use a shared method for both. I'd suggest something like this in your service class:
Here
_clientCacheis your injectedIFlurlClientCachesingleton.GetOrAddis thread-safe and ensures that the builder action happens lazily and at most once per client name; no need for locks here. All other methods in your class can simply callGetOrCreateClient(...).Request(...)as needed to invoke Flurl calls, and no matter how often that happens, theGetOrAddbehavior ensures that no more than 2 clients are created/configured for the life of your application.