JasperFx / lamar

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

Issue resolving service that takes IMemoryCache #213

Closed davemartindale closed 4 years ago

davemartindale commented 4 years ago

I am having some issues when trying to resolve a class that uses IMemoryCache, Is this a bug with Lamar or am I just doing something wrong here?

I have the following class

public class DummyManager : IDummyManager
{
    private readonly IMemoryCache MemoryCache;

    public DummyManager(IMemoryCache memoryCache)
    {
        this.MemoryCache = memoryCache;
    }

    public String Get()
    {
        return "Hello World";
    }
}

and I have this in my ConfigureContainer (just doing the resolve here for testing)

public void ConfigureContainer(ServiceRegistry services)
{
    services.AddControllers();

    services.AddMemoryCache();

    services.For<IDummyManager>().Use<DummyManager>().Singleton();

    services.CheckLamarConfiguration();

    ServiceProvider provider = services.BuildServiceProvider();

    IDummyManager dummyManager = provider.GetService<IDummyManager>();
}

When it tries to resolve IDummyManager it throws the following error

{"Constant value of type 'Lamar.IoC.Instances.ConstructorInstance`1[LamarDI.Controllers.DummyManager]' can't be converted to service type 'LamarDI.Controllers.IDummyManager'"}

My Program.cs looks like this

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseLamar()
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}
jeremydmiller commented 4 years ago

@davemartindale Sorry for how slow I've been to respond here. This like of code is your culprit:

ServiceProvider provider = services.BuildServiceProvider();

You're not even using Lamar to do the resolution there, and the built in DI container isn't going to understand the Lamar centric registration.