Repository navigation
JsonObjectsWithHttpClient: net10.0, three-argument StringContent, JsonContent and source-generated samples - #2274
Open
vladimir-pecanac-main wants to merge 1 commit into
Conversation
…nContent and source-generated samples - Retarget both projects from net8.0 to net10.0. - Replace Swashbuckle.AspNetCore 6.4.0 with Microsoft.AspNetCore.OpenApi 10.0.12 (AddOpenApi, MapOpenApi); drop the empty RunConfiguration PropertyGroup; launchUrl now openapi/v1.json. - Delete the dead AddScoped registration (AddHttpClient registers the typed client). - PetService: primary constructor, three-argument StringContent, ReadFromJsonAsync on every path, rename PostAsJsonAsync to PostWithPostAsJsonAsync. - Add PostAsJsonContentAsync (JsonContent.Create plus PostAsync) and PostAsSourceGeneratedJsonAsync (PetContext), with routes /postAsJsonContent and /postAsSourceGeneratedJson. - Tests: async Task instead of async void (xUnit1048), shared CreateClient helper, a test per service method, and a test pinning the two StringContent constructors' Content-Type values. - Test packages: Microsoft.NET.Test.Sdk 18.10.1, xunit 2.9.3, xunit.runner.visualstudio 4.0.0, coverlet.collector 10.1.0, Moq 4.20.70 down to 4.18.4 (SponsorLink).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sample update for the article "How to Send a JSON Object Using HttpClient in .NET" (https://code-maze.com/dotnet-how-to-send-a-json-object-using-httpclient/). Only
dotnet-client-libraries/JsonObjectsWithHttpClientchanges.Changes:
AddOpenApi(),MapOpenApi()in Development). The emptyRunConfigurationPropertyGroup is removed andlaunchUrlpoints atopenapi/v1.json.AddScoped<IPetService, PetService>()line is removed:AddHttpClient<IPetService, PetService>()already registers the typed client, and its registration is the one the container resolves.PetService: primary constructor;new StringContent(pet, Encoding.UTF8, "application/json")replaces the two-argument constructor plus the separateContentTypeassignment;ReadFromJsonAsyncreplacesReadAsStringAsyncplusJsonSerializer.Deserialize; the service methodPostAsJsonAsync()is renamedPostWithPostAsJsonAsync()so it no longer shares a name with the extension method it calls.PostAsJsonContentAsync()(JsonContent.CreateplusPostAsync) andPostAsSourceGeneratedJsonAsync()(PetContext.Default.PetDtoon both the request and the response), with routes/postAsJsonContentand/postAsSourceGeneratedJson, andPetContext.cs.async Taskinstead ofasync void(xUnit1048 on xunit 2.9.3), oneCreateClient()helper, a test per service method asserting theContent-Typethat was sent, and a test pinningtext/plain; charset=utf-8for the two-argumentStringContentconstructor andapplication/json; charset=utf-8for the three-argument one.Packages, re-queried on NuGet on 2026-10-09 (latest listed stable unless noted):
Verified locally on .NET SDK 10.0.302, runtime 10.0.10:
dotnet build -c Release0 warnings, 0 errors;dotnet test5 passed, 0 failed;dotnet list package --vulnerable --include-transitivereports no vulnerable packages. All four routes were run against https://petstore.swagger.io/v2 and returned 200.