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
Conversation
…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.
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 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
net7.0tonet10.0.Swashbuckle.AspNetCorereference,AddEndpointsApiExplorer(),AddSwaggerGen(),UseSwagger()andUseSwaggerUI().AddOpenApi()andMapOpenApi()replace them inside the existingIsDevelopment()block, as the .NET 10webapitemplate emits them.Properties/launchSettings.json:launchUrlwasswagger, a page that no longer exists once Swashbuckle is removed. It now opensapi/User, the sample's endpoint.Program.cscomment aboveAddScoped<IUserService, UserService>()said "as a singleton". It now says "with a scoped lifetime", which is what the line does.string:Settings/SmtpSettings.cs, anSmtpsection inappsettings.json,Configure<SmtpSettings>(...)inProgram.cs, andServices/EmailService.cstakingIOptions<SmtpSettings>.HttpClientorIHttpClientFactory:AddHttpClient()plus aServices/WeatherClient.cstyped client registered withAddHttpClient<WeatherClient>(...).Services/ServiceResolutionTest.csadds one test per new scenario, resolving the services from the built app.UserControllerIntegrationTestnow disposes itsHttpClientandWebApplicationFactoryin a[OneTimeTearDown]. NUnit.Analyzers 4.15.0 reports NUnit1032 as a build error without it. The test's assertions are unchanged. The fixture is alsopublicnow. That change is cosmetic: NUnit 4 discovered and ran it asinternaltoo.Package versions (re-queried on NuGet 2026-09-23)
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.OpenApi10.0.10 (the version the template pins) raises NU1903 for a transitiveMicrosoft.OpenApi. 10.0.12 restores clean.Verified locally
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.