Refactoring Similar Network Requests
I have 3 async methods with a great deal of similarities but some differences (some use Authorization token, some have Json payload, some return Task<bool>, others Task) but all make network requests in a similar flow.
What is an approach I could use to refactor them?
40 Replies
many issues here. what .net version are you on? is there a specific reason you use newtonsoft.json instead of system.text.json? don't create a new httpclient for every request. lots of things here are a bit overcomplicated, but we can work through them
you need to first fix the two points mentioned before refactoring any of this
then get back to us
how much to refactor also depends from what are you building
I am on .NET 2.1
I wantes to use system.text.json too
How to not create a new Httpclient for every request? Do I just store it as a class variable?
Which two
is there a reason you're using ns2.1?
not using HttpClient
and not using newtonsoft
either a static httpclient or httpclientfactory
in heavy load you'll run into problems otherwise
Pre existing project, I wish it were easy to upgrade too
Its not my call
and you just dont want to using HttpClient, it's bad
Ok, so I store it as a class variable and reuse it right
When should I kill off the object instance or do I leave it running with the program
you'll have a hard time upgrading this properly on ns2.1
everything that makes this better is only accessible as a package
IHttpClientFactory
is only really applicable if you have a proper builder to launch your app
which itself is a lot of workCould you name some examples anyway? I'm keen to learn
for the httpclient, you'll need to decide the lifetime on your own. you could make your class implement IDisposable and dispose the client there.
you can pull in the
System.Net.Http.Json
package for some convenience methods (pulls in system.text.json transitively)Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
...what makes those projects weird?
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
Its aspnetcore
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
okay yeah then this is a really weird approach lol
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
No winforms or uwp idk what those are even
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
You can?
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
Ok if i chose aspnetcore 6.0 what would be the next steps after that
to update to aspnetcore 9.0 in 4 days
because 6.0 would be out of support
Ok but what do I do about the httpclient and web requests lol
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
and like the screenshot shows, you just use
SendAsJsonAsync
and GetFromJsonAsync
of course this will require you to have some concrete models to de/serialize
say
await _client.SendAsJsonAsync(request)
Oh records are a thing in c#? I'd only ever heard about them in Java
Apologies, I'm not familiar with DI in .net CORE
I come from Java land where we need [@Bean] or [@Resource] Spring boot
How different is the configuration because this looks like DI injected HttpClient if I am not mistaken?
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
I assume factory is no longer needed in this case?
When would factory still be needed
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
My net is dead
But i will definitely read it once i get back
remember we're only helping you, not bashing on your stuff
we want your code better, so do you
i'm sceptical about how much of this actually helps if upgrading the tfm is really not their call
It's ok, I want to learn
I am only in this bc i wanted a progression step before seeking opportunities at a real .NET shop
I am happy for all the resources, and will practice on my own even if i can't apply any of it right now
Yep, no worries, sorry if I didn't reply to everything yet