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

Upgrade from 4.2.2 to 5.0.1 breaks decoration #237

Open
lukasvavrek opened this issue Oct 26, 2024 · 5 comments
Open

Upgrade from 4.2.2 to 5.0.1 breaks decoration #237

lukasvavrek opened this issue Oct 26, 2024 · 5 comments
Labels

Comments

@lukasvavrek
Copy link

Hi, while playing around updating and upgrading my app I run into a compatibility issue.

Here is an example of the registration code:

public static class DependencyInjection
{
    public static IServiceCollection AddApplication(this IServiceCollection services)
    {
        services.Scan(scan =>
            scan.FromCallingAssembly()
                .AddClasses(classes => classes
                    .AssignableTo(typeof(IRequestHandler<,>))
                    .NotInNamespaceOf<IRequestHandlerBehavior>())
                .AsImplementedInterfaces()
                .WithTransientLifetime()
        );
        
        services.Decorate(typeof(IRequestHandler<,>), typeof(RequestExceptionHandler<,>));

        return services;
    }
}

While this works as expected in version 4.2.2, in 5.0.1 I am running into:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.Populate()
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
   at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()
   at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
   at Program.<Main>$(String[] args) in /XXX/Program.cs:line 15

Let me please know if I can provide some useful information!

@khellang
Copy link
Owner

Thanks for the bug report! Looks like it comes from deep within the container code. Need to dig a bit here 😅

@Mike-E-angelo
Copy link

Related: #235

@ajdean
Copy link

ajdean commented Nov 27, 2024

From what I have found if you are targeting net8.0 then decoration works fine.

If you are targeting something below that (e.g. net6.0 or net7.0), then you will need to ensure you have added Microsoft.Extensions.DependencyInjection version >= 8.0.0 otherwise I think it fails due to keyed services not being supported.

@martynas-m
Copy link

We're using .net 8, and tried to upgrade to 5.0.2- it crashes with same Object reference not set to an object error. But we migrated DependencyInjection package to 9th version.

@khellang khellang added the Bug label Dec 23, 2024
@khellang
Copy link
Owner

I have tests covering pretty much this exact scenario, which passes on latest Scrutor main, targeting .NET 9 with Microsoft.Extensions.DependencyInjection v9.0.0.

Does anyone have any additional details that could help pin this down?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants