-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
DI behavior change between .NET 7.0 and .NET 8.0 #88968
Comments
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection Issue DetailsDescriptionThe snippet attached to this ticket works fine on .NET 7.0 (and earlier) but crashes on .NET 8.0 preview6 (changing Reproduction Steps<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-preview.6.23329.7" />
<PackageReference Include="Moq" Version="4.18.4" />
</ItemGroup>
</Project> using Microsoft.Extensions.DependencyInjection;
using Moq;
var mock = Mock.Of<IInterface>();
var services = new ServiceCollection();
services.AddSingleton(typeof(IInterface), mock.GetType());
await using var provider = services.BuildServiceProvider();
var instance = provider.GetRequiredService<IInterface>();
Console.WriteLine($"Instance has the correct type: {instance.GetType() == mock.GetType()}");
public interface IInterface { } Expected behaviorNo exception, Actual behavior
Regression?Yes, working fine on .NET 7.0 and earlier. Known WorkaroundsNo response ConfigurationNo response Other informationChanging the
|
Did not reproduce with Preview 4, just installed Preview 6 and it does. Also reproduces with version 7.0.0 of MS.E.DI. Here's the full stack trace:
Seems like a reflection change that broke DI. @dotnet/area-system-reflection thoguhts? |
I believe this was caused by #84550 which fixes an issue with emit that used to add a default value of The Moq-generated object contains only 1 ctor with the following args:
which are not emit'd with default values. I haven't reseached Moq to see if there is some sort of feature to generate a default ctor for implemented interfaces. For the super simple sample provided in the issue, adding: var mock = new Mock<IInterfaceImpl>().Object;
public class IInterfaceImpl : IInterface { } shows the issue - the class will now contain a default ctor. I also added the "needs breaking change doc" flag to that issue. |
@steveharter thanks for taking the time to investigate, much appreciated! 😃 The suggested fix is pretty much what I had in mind but I preferred reporting this issue first, given that this code has always worked fine on .NET Framework 4.6.1/4.7.2/4.8 and every .NET Core version from 1.0 to 7.0. If the outcome is "it worked, but only by accident", I'm fine with that 👍🏻 Thanks again! |
I'm going to close this as by design, and due to the breaking change above. If DI needs to work around this issue, please re-open or comment here. |
Description
The snippet attached to this ticket works fine on .NET 7.0 (and earlier) but crashes on .NET 8.0 preview6 (changing
<TargetFramework>net8.0</TargetFramework>
to<TargetFramework>net7.0</TargetFramework>
is enough to stop the exception).Reproduction Steps
Expected behavior
No exception,
Instance has the correct type: True
visible in the console output.Actual behavior
Regression?
Yes, working fine on .NET 7.0 and earlier.
Known Workarounds
No response
Configuration
No response
Other information
Changing the
Microsoft.Extensions.DependencyInjection
package version doesn't seem to have any influence.The text was updated successfully, but these errors were encountered: