Description
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