Skip to content

JsonObjectsWithHttpClient: net10.0, three-argument StringContent, JsonContent and source-generated samples - #2274

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/109779-dotnet-how-to-send-a-json-object-using-httpclient
Open

vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/109779-dotnet-how-to-send-a-json-object-using-httpclient

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

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/JsonObjectsWithHttpClient changes.

Changes:

  • Both projects retargeted from net8.0 to net10.0.
  • Swashbuckle.AspNetCore 6.4.0 replaced by Microsoft.AspNetCore.OpenApi 10.0.12 (AddOpenApi(), MapOpenApi() in Development). The empty RunConfiguration PropertyGroup is removed and launchUrl points at openapi/v1.json.
  • The 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 separate ContentType assignment; ReadFromJsonAsync replaces ReadAsStringAsync plus JsonSerializer.Deserialize; the service method PostAsJsonAsync() is renamed PostWithPostAsJsonAsync() so it no longer shares a name with the extension method it calls.
  • New PostAsJsonContentAsync() (JsonContent.Create plus PostAsync) and PostAsSourceGeneratedJsonAsync() (PetContext.Default.PetDto on both the request and the response), with routes /postAsJsonContent and /postAsSourceGeneratedJson, and PetContext.cs.
  • Tests: async Task instead of async void (xUnit1048 on xunit 2.9.3), one CreateClient() helper, a test per service method asserting the Content-Type that was sent, and a test pinning text/plain; charset=utf-8 for the two-argument StringContent constructor and application/json; charset=utf-8 for the three-argument one.

Packages, re-queried on NuGet on 2026-10-09 (latest listed stable unless noted):

  • Microsoft.AspNetCore.OpenApi 10.0.12
  • Microsoft.NET.Test.Sdk 18.10.1
  • xunit 2.9.3
  • xunit.runner.visualstudio 4.0.0
  • coverlet.collector 10.1.0
  • Moq 4.18.4 (deliberately below the latest 4.21.0: versions from 4.20.0 carry SponsorLink)

Verified locally on .NET SDK 10.0.302, runtime 10.0.10: dotnet build -c Release 0 warnings, 0 errors; dotnet test 5 passed, 0 failed; dotnet list package --vulnerable --include-transitive reports no vulnerable packages. All four routes were run against https://petstore.swagger.io/v2 and returned 200.

…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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant