Refactoring Injected Singleton for Authentication and HttpClient Usage
Everytime a
TestPaymentsService
is created, it should be authenticated. Instead of constantly calling .Authenticate, I want to be able to do it once at the start of my app's lifetime.
Instead of firing off a web request in the constructor (since it cannot be async), I decided to create a static create method that creates the instance to be able to authenticate.
However, I now realised its a bad practice to recreate a HttpClient in every method inside TestPaymentsService
. Although I registered a IHttpClientFactory
using builder.Services.AddHttpClient();
in Program.cs
, I cannot inject it since I dont have a constructor.
I want to be able to create a TestPaymentsService
, inject it into DI and use it wherever I want without having to call Authenticate
method each time, since it was called at the very beginning.
How should I approach this? Current implementation:
Program.cs:
TestPaymentsService.cs:
2 Replies
Fixed like so... any objections?
Youll need to open up a scope, but, instead I would put a private method on your service called something like
EnsureAuthenticatedAsync()
and just call it at the start of your methods.
If it is already authenticated it just returns and short circuits out.
This is good practice because, chances are, later on you'll be able to easily adapt to things like "what if your authentication method can expire?"
Its common for bearer tokens or whatever you use to authenticate to have an expiry attached, you'd then be able to track that as a private variable and your Ensure method can check that it's old token/cookie/etc isn't expired as well as part of its logic, and if it is expired, renew it