Skip to content

WebApplicationFactory.Services throws Exception with new UseKestrel API #62224

Closed
@abcfantasy

Description

@abcfantasy

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

A NullReferenceException is thrown when trying to access the WebApplicationFactory.Services property, and when using the new UseKestrel api (added here #60635).

This happens when the application being tested is using the WebApplicationBuilder to initialize.

If I understand the code correctly, then the issue might be in the StartServer() logic. If the app being tested does not have a static CreateWebHostBuilder method, then the _webHost variable is never initialized, and the _host variable is initialized instead.

Then, the Services property is trying to access _webHost!.Services which is the line that throws the exception.

Expected Behavior

No response

Steps To Reproduce

Github repo to reproduce: https://github.com/abcfantasy/WafUseKestrelIssue

Running dotnet test on that solution will cause the NullReferenceException.

Exceptions (if any)

 System.NullReferenceException : Object reference not set to an instance of an object.
      Stack Trace:
         at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.get_Services()
         at Api.Tests.UnitTest1.InitializeAsync() in ...\UnitTest1.cs:line 18

.NET Version

10.0.100-preview.4.25258.110

Anything else?

No response

Metadata

Metadata

Labels

area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-mvc-testingMVC testing package

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions