Skip to content
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 #102888

Closed
snowinmars opened this issue May 30, 2024 · 5 comments
Closed

DI behavior change between .NET 7.0 and .NET 8.0 #102888

snowinmars opened this issue May 30, 2024 · 5 comments

Comments

@snowinmars
Copy link

Description

Still have the issue on dotnet 8.

See #88968

Reproduction Steps

Reproduction repository: https://github.com/snowinmars/Dotnet8DiBugCase/tree/master

Expected behavior

See in the reproduction repository

Actual behavior

See in the reproduction repository

Regression?

No response

Known Workarounds

No response

Configuration

NET7 > NET8

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label May 30, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 30, 2024
@vcsjones vcsjones added area-Extensions-DependencyInjection and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels May 30, 2024
@ericstj
Copy link
Member

ericstj commented Jun 18, 2024

#88968 was closed as by design and the change was documented. You'll need to share more details here.

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.

If this was the case you'll need to add more details here to help us understand what you need.

@ericstj ericstj added the needs-author-action An issue or pull request that requires more info or actions from the author. label Jun 18, 2024
@snowinmars
Copy link
Author

The #88968 is about moq di. Current example doesn't involve moq, so maybe it's a different issue. Isn't it?

I don't see any breaking changes doc, attached to the #88968 . Where can I read them?

@dotnet-policy-service dotnet-policy-service bot removed the needs-author-action An issue or pull request that requires more info or actions from the author. label Jun 18, 2024
@steveharter
Copy link
Member

The breaking change doc is at dotnet/docs#36725.

@snowinmars-debug
Copy link

Yea, I saw this doc before. There is an issue: neither I nor Castle afais uses HasDefaultValue property on DefineConstructor()/DefineMethod() result.

@ericstj ericstj added investigate and removed untriaged New issue has not been triaged by the area owner labels Jul 18, 2024
@ericstj ericstj added this to the 9.0.0 milestone Jul 18, 2024
@steveharter
Copy link
Member

Castle uses IL.Emit which in v6 and earlier used to add a null default value to each argument of a method. Starting in v7, IL.Emit doesn't do that by default.

Closing this issue. A Castle bug should be created so it can be fixed for that. The fix would be to either add an empty constructor, or add default values to arguments via [Optional, DefaultParameterValue(null)] attributes.

Running this below shows the behavior difference:

            var services = new ServiceCollection();

            services.AddScopedWithInterceptors<ITestService, TestService, TestInterceptor>();
            var b = services.BuildServiceProvider();

            ServiceDescriptor sd = services[1];
            // v6:
            // Implementation type: ITestServiceProxy
            // Orig Arg 1 serviceType False
            // Impl Arg 1  True
            // Orig Arg 2 serviceKey False
            // Impl Arg 2  True
            // Orig Arg 3 implementationType False
            // Impl Arg 3  True

            // v7+:
            // Implementation type: ITestServiceProxy
            // Orig Arg 1 serviceType False
            // Impl Arg 1  False
            // Orig Arg 2 serviceKey False
            // Impl Arg 2  False
            // Orig Arg 3 implementationType False
            // Impl Arg 3  False
            Console.WriteLine($"Implementation type: {sd.ImplementationType!.Name}");
            Console.WriteLine($"Orig Arg 1 {sd.GetType().GetConstructors()[1].GetParameters()[0].Name} {sd.GetType().GetConstructors()[1].GetParameters()[0].HasDefaultValue}");
            Console.WriteLine($"Impl Arg 1 {sd.ImplementationType.GetConstructors()[0].GetParameters()[0].Name} {sd.ImplementationType.GetConstructors()[0].GetParameters()[0].HasDefaultValue}");
            Console.WriteLine($"Orig Arg 2 {sd.GetType().GetConstructors()[1].GetParameters()[1].Name} {sd.GetType().GetConstructors()[1].GetParameters()[1].HasDefaultValue}");
            Console.WriteLine($"Impl Arg 2 {sd.ImplementationType.GetConstructors()[0].GetParameters()[1].Name} {sd.ImplementationType.GetConstructors()[0].GetParameters()[1].HasDefaultValue}");
            Console.WriteLine($"Orig Arg 3 {sd.GetType().GetConstructors()[1].GetParameters()[2].Name} {sd.GetType().GetConstructors()[1].GetParameters()[2].HasDefaultValue}");
            Console.WriteLine($"Impl Arg 3 {sd.ImplementationType.GetConstructors()[0].GetParameters()[2].Name} {sd.ImplementationType.GetConstructors()[0].GetParameters()[2].HasDefaultValue}");


            var service = b.GetService<ITestService>();
            System.Console.WriteLine(service.DoTest());

@steveharter steveharter closed this as not planned Won't fix, can't repro, duplicate, stale Aug 6, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Sep 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants