DevTeam / Pure.DI

Pure DI for .NET
MIT License
410 stars 21 forks source link

Confusing error if parentheses are used when registering a factory method #35

Closed AtomicBlom closed 6 months ago

AtomicBlom commented 6 months ago

Hi there,

I just spent a good half an hour trying to figure out what I was doing wrong registering a factory method

.Bind<ILoggerFactory>().As(Lifetime.Singleton).To((ctx) =>
{
    ctx.Inject(out ILogger logger);
    return new LoggerFactory()
        .AddSerilog(logger);
})

This code does not compile with the error

Error   DIE003  
The DI.Setup(nameof(CompositionRoot))
        .Bind<ILoggerFactory>().As(Lifetime.Singleton).To((ctx) =>
        {
            ctx.Inject(out ILogger logger);
            return new LoggerFactory()
                .AddSerilog(logger);
        }) is not supported.

After a bit of playing around, it seems that the problem is actually the parenthesis (ctx) => { } in the registration, as they interfere with the source generation and removing them allows the source generator to continue correctly.

Admittedly they're completely superfluous, but I honestly didn't even notice they were there in the sea of red squiggles in Visual Studio.

Environment information: .net 8.0.101 C# LangVersion default (11) Pure.DI version 2.0.45 (latest as of this report)

NikolayPianikov commented 6 months ago

Thanks for reporting this bug and for finding a workaround. Fixed in version 2.0.46. In case of problems with version update, please pay attention to the Version update part.