Skip to content

Unable to resolve service: retarget net10.0, drop Swashbuckle, add string and HttpClient scenarios - #2228

Open
vladimir-pecanac-main wants to merge 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/92210-dotnet-how-to-solve-unable-to-resolve-service-for-a-type
Open

vladimir-pecanac-main wants to merge 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/92210-dotnet-how-to-solve-unable-to-resolve-service-for-a-type

Conversation

@vladimir-pecanac-main

@vladimir-pecanac-main vladimir-pecanac-main commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Sample for the article "Unable to Resolve Service for Type: How to Fix It in .NET" (code-maze.com/dotnet-how-to-solve-unable-to-resolve-service-for-a-type/). Touches only dotnet-dependency-injection/FixUnableToResolveServiceIssue.

Changes

  • Both projects retargeted from net7.0 to net10.0.
  • Swashbuckle removed: the Swashbuckle.AspNetCore reference, AddEndpointsApiExplorer(), AddSwaggerGen(), UseSwagger() and UseSwaggerUI(). AddOpenApi() and MapOpenApi() replace them inside the existing IsDevelopment() block, as the .NET 10 webapi template emits them.
  • Properties/launchSettings.json: launchUrl was swagger, a page that no longer exists once Swashbuckle is removed. It now opens api/User, the sample's endpoint.
  • The Program.cs comment above AddScoped<IUserService, UserService>() said "as a singleton". It now says "with a scoped lifetime", which is what the line does.
  • New sample code for the two scenarios the article adds:
    • A constructor that asks for a plain string: Settings/SmtpSettings.cs, an Smtp section in appsettings.json, Configure<SmtpSettings>(...) in Program.cs, and Services/EmailService.cs taking IOptions<SmtpSettings>.
    • A constructor that asks for HttpClient or IHttpClientFactory: AddHttpClient() plus a Services/WeatherClient.cs typed client registered with AddHttpClient<WeatherClient>(...).
  • Tests: Services/ServiceResolutionTest.cs adds one test per new scenario, resolving the services from the built app.
  • UserControllerIntegrationTest now disposes its HttpClient and WebApplicationFactory in a [OneTimeTearDown]. NUnit.Analyzers 4.15.0 reports NUnit1032 as a build error without it. The test's assertions are unchanged. The fixture is also public now. That change is cosmetic: NUnit 4 discovered and ran it as internal too.

Package versions (re-queried on NuGet 2026-09-23)

Package Before After
Microsoft.AspNetCore.OpenApi 7.0.4 10.0.12
Swashbuckle.AspNetCore 6.4.0 removed
Microsoft.AspNetCore.Mvc.Testing 7.0.8 10.0.12
Microsoft.NET.Test.Sdk 17.3.2 18.10.1
NUnit 3.13.3 4.6.1
NUnit3TestAdapter 4.3.0 6.3.0
NUnit.Analyzers 3.5.0 4.15.0
coverlet.collector 3.1.2 10.0.1

The batch plan named Microsoft.NET.Test.Sdk 18.10.0. The re-query found 18.10.1, and that is the version used. Microsoft.AspNetCore.OpenApi 10.0.10 (the version the template pins) raises NU1903 for a transitive Microsoft.OpenApi. 10.0.12 restores clean.

Verified locally

  • SDK 10.0.302, runtime .NET 10.0.10.
  • dotnet build -c Release: 0 warnings, 0 errors.
  • dotnet test -c Release --filter "FullyQualifiedName!~Live": 3 passed, 0 failed.
  • dotnet list package --vulnerable --include-transitive: no vulnerable packages in either project.

…ring and HttpClient scenarios

- Both projects net7.0 to net10.0.
- Swashbuckle.AspNetCore removed with AddEndpointsApiExplorer, AddSwaggerGen, UseSwagger and
  UseSwaggerUI; AddOpenApi and MapOpenApi in their place, as the .NET 10 template has them.
- Microsoft.AspNetCore.OpenApi and Microsoft.AspNetCore.Mvc.Testing 10.0.12, NUnit 4.6.1,
  NUnit3TestAdapter 6.3.0, NUnit.Analyzers 4.15.0, Microsoft.NET.Test.Sdk 18.10.1,
  coverlet.collector 10.0.1.
- Program.cs comment now says the service is registered with a scoped lifetime, which is what
  the AddScoped line under it does.
- New string scenario: SmtpSettings options class, an Smtp section in appsettings.json, and an
  EmailService that asks for IOptions<SmtpSettings> instead of a plain string.
- New HttpClient scenario: AddHttpClient() plus a WeatherClient typed client.
- One test per new scenario resolving the services from the built app.
- UserControllerIntegrationTest disposes its client and factory in a OneTimeTearDown
  (NUnit.Analyzers 4.15.0 reports NUnit1032 as an error without it) and is now public.
With Swashbuckle gone, launchUrl "swagger" opened a page that no longer exists.
The profile now opens GET api/User, the sample's endpoint.
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