C
C#•2mo ago
Camster

Service Provider Question

I have a Blazor WASM intranet site with a backend API. I would like to have a really easy way of switching between dev/test/production api, where both the URI and the Name can be utilized in blazor. I created an APIConnection class, which has three classes that implement for each of dev/test/production (1st screenshot). Then I have added that class to service provider (2nd sceenshot). I can easily change the name of the class when I need to switch. However, I'm having difficulty using that scoped instance to send the URI to the HttpClient. How do I access the scoped instance of the APIConnection inside the scoped instance of HttpClient (3rd sceenshot)?
No description
No description
No description
9 Replies
Pobiega
Pobiega•2mo ago
Your ApiConnectionX instances dont need to be scoped, they have no state at all
Camster
CamsterOP•2mo ago
Mainly I'd like to show the name somewhere on the blazor pages, so the user knows which api is being used.
Pobiega
Pobiega•2mo ago
they can safely be Singleton instead, and you'd use it by simply putting an IAPIConnection connection or similar in your constructor of Client
Camster
CamsterOP•2mo ago
I guess I'm not sure how to access it from within the lambda for the httpclient Regardless scoped or singleton, if that makes sense
Pobiega
Pobiega•2mo ago
modify Client you'd need to take in either the API connection (recommended) or a serviceprovider reference into Client itself since thats what you have access to in the lambda
Camster
CamsterOP•2mo ago
Okay, thank you. I'll give that a try
Sir Rufo
Sir Rufo•2mo ago
Please no image from $code
MODiX
MODiX•2mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Camster
CamsterOP•2mo ago
This worked! Thank you for the help 🙂

Did you find this page helpful?