JasperFx / lamar

Fast Inversion of Control Tool and Successor to StructureMap
https://jasperfx.github.io/lamar
MIT License
571 stars 119 forks source link

Lambda creation doesn't work #293

Closed jansssson closed 3 years ago

jansssson commented 3 years ago

Trying to configure a factory for creating my custom ISystemLogger objects, but it doesn't work. First problem, the documentation says there should be four signatures for creating lambdas (https://jasperfx.github.io/lamar/guide/ioc/lambdas.html), but I only see one, which isn't any of the four in doc:

Second problem, using that signature, I get an exception during HostBuilder.Build (using asp.net core 5 integration), InvalidOperationException, with an inner ArgumentException: Constant value of type 'Lamar.IoC.Instances.LambdaInstance`2[Lamar.IServiceContext,ISystemLogger]' can't be converted to service type 'ISystemLogger'

The lambda itself is as easy as the example in the doc: For().Use(context => context.GetInstance().CreateLogger());

Am I doing something wrong, or is LambdaInstance broken? And what about the doc's?

jeremydmiller commented 3 years ago

I really don't think the real functionality is broken, and all the tests are passing as of a few minutes ago. Can you share a little more detail about how you're using it?

As for the docs, meh, that's old content from StructureMap that was copied in and the source code is even coming from Struc tureMap, so yeah, that's not quite right.

jeremydmiller commented 3 years ago

Actually, can you put the real stack trace in here? Dollars to donuts, the problem you're getting is that Lamar isn't the container and you're getting an error from Lamar artifacts trying to be used by the built in DI container.

jansssson commented 3 years ago

Turns out you're right. I commented out the lambda, and continued with my tests. Once I also added a ConfigureContainer(...), I got a casting problem similar to what's described in #272 . What I had to do was to move .UseLamar() from inside .ConfigureWebHostDefaults to outside.

Just doublechecked, the doc's are correct on that point, I had just placed the call incorrectly. I previously had this, which didn't work:

            return Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseLamar();
                    webBuilder.UseStartup<Startup>();
                });

when I changed to the following, both ConfigureContainer(...) and the lambda started working correctly.

            return Host.CreateDefaultBuilder(args)
                .UseLamar()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });