Skip to content

[MVC.Testing] Fix NullReferenceException #62231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

martincostello
Copy link
Member

Fix NullReferenceException from WebApplicationFactory.Services

Fix NullReferenceException from WebApplicationFactory.Services.

Description

Fix NullReferenceException accessing Services in a web application using top-level programs and add tests.

Fixes #62224

Fix `NullReferenceException` accessing `Services` in a web application using top-level programs.

Fixes dotnet#62224.
@Copilot Copilot AI review requested due to automatic review settings June 3, 2025 18:23
@martincostello martincostello requested a review from a team as a code owner June 3, 2025 18:23
@github-actions github-actions bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jun 3, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jun 3, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a NullReferenceException in WebApplicationFactory.Services by updating the service resolution logic and adding tests for minimal and real server scenarios.

  • Removed an unused using directive in RealServerWithDynamicPortIntegrationTests.cs.
  • Added integration tests for minimal-backed and real server scenarios.
  • Updated the Services getter in WebApplicationFactory.cs to handle potential null values.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Mvc/test/Mvc.FunctionalTests/RealServerWithDynamicPortIntegrationTests.cs Removed an unused using directive.
src/Mvc/test/Mvc.FunctionalTests/RealServerUsingMinimalBackedIntegrationTests.cs Added minimal-backed integration tests including service resolution.
src/Mvc/test/Mvc.FunctionalTests/RealServerBackedIntegrationTests.cs Removed redundant base address assignment and added a test for resolving services.
src/Mvc/test/Mvc.FunctionalTests/KestrelBasedWebApplicationFactoryForMinimal.cs Introduced a new web application factory utilizing a dynamically assigned port.
src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs Updated the Services getter to fallback to _host.Services when _webHost is null.

@captainsafia captainsafia merged commit 71df2cc into dotnet:main Jun 5, 2025
28 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview6 milestone Jun 5, 2025
@martincostello martincostello deleted the gh-62224-fix-waf-for-kestrel branch June 5, 2025 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates community-contribution Indicates that the PR has been added by a community member feature-mvc-testing MVC testing package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WebApplicationFactory.Services throws Exception with new UseKestrel API
2 participants