Strange behavior with DI and optional constructor parameters
Describe the bug
I am registering a class, with an optional parameter in the constructor, to the service collection.
For some reason it starts getting constructed with a arbitrary value after a number of constructions.
It seems very strange and I am not sure if it is only a problem with my machine.
To Reproduce
I have created a small test solution for reproducing the problem. DependencyInjectionBug.zip
If you run it and call the TestController.TestMethod endpoint it will return the default value. But after running it three times it starts returning a seemingly arbitrary value.
Further technical details
- ASP.NET Core version ASP.NET Core 3.1
- Include the output of
dotnet --info
.NET Core SDK (reflecting any global.json): Version: 3.1.100 Commit: cd82f021f4
Runtime Environment: OS Name: Windows OS Version: 10.0.18362 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.1.100\
Host (useful for support): Version: 3.1.0 Commit: 65f04fb6db
.NET Core SDKs installed: 3.1.100 [C:\Program Files\dotnet\sdk]
.NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.0 [C:\Program >Files\dotnet\shared\Microsoft.WindowsDesktop.App]
- The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version Microsoft Visual Studio Community 2019 Version 16.4.2
This should be fixed in 5.0 by https://github.com/dotnet/extensions/pull/2501 . We're discussing backporting it to 3.1 in https://github.com/dotnet/extensions/issues/2431.
Closing as this is a duplication.