JasperFx / lamar

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

ILogging not working from Integration Test project in ASP.Net Core 3.1 solution #223

Closed TWJH closed 4 years ago

TWJH commented 4 years ago

Hello,

I am in the process of migrating an existing aspnet core solution to .NET Core 3.1 from 2.2 which previously utilised StructureMap. I have moved over to Lamar as part of the migration as StructureMap doesn't work with 3.1.

The aspnet application has been migrated and works fine, however I am using Jimmy Bogard's MediatR exclusively as part of the vertical slice architecture for the app. As part of this, I have an integration test project which has a SliceFixture.cs static class which invokes the Startup.cs class in the aspnet application as part of its lifecycle calling all of the requisite startup code for the tests to run correctly.

The problem I am having with Lamar is that it doesn't have any of the ILogger dependencies registered when I execute any integration tests on classes which depend on an ILogger.

The error I get is

System.InvalidOperationException : Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger`1[Web.Features.Maintenance.HearingLoss.SortOrder+CommandHandler]' while attempting to activate 'Web.Features.Maintenance.HearingLoss.SortOrder+CommandHandler'.

This is the scanning code I have in my startup:

services.Scan(_ =>
{
       _.AssemblyContainingType(typeof(Startup));
       _.WithDefaultConventions();
       _.SingleImplementationsOfInterface();
       _.ConnectImplementationsToTypesClosing(typeof(IRequestHandler<,>));
       _.ConnectImplementationsToTypesClosing(typeof(INotificationHandler<>));
});

services.For<IRazorLightEngine>()
     .Use(RazorLightEngineFactory.Get())
     .Singleton();

if (configuration.IsAutomatedTesting())
     services.For<INotifier>().Use<FakeSmtpNotifier>();
else
     services.For<INotifier>().Use<SmtpNotifier>();

     services.For<IConfiguration>().Use(configuration);

     services.For<IMediator>().Use<Mediator>().Transient();
     services.For<ServiceFactory>().Use(ctx => ctx.GetInstance);

The results of WhatDidIScan() from both the aspnet app and the intergration tests are exactly the same:

All Scanners

Assemblies

  • Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
  • Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Conventions

  • Default I[Name]/[Name] registration convention
  • Register any single implementation of any interface against that interface
  • Connect all implementations of open generic type MediatR.IRequestHandler`2
  • Connect all implementations of open generic type MediatR.INotificationHandler`1

No problems were encountered in exporting types from Assemblies

The results from WhatDoIHave() method from running from the aspnet application show all of the ILogger dependencies have been registered but not so when I run the integration tests.

TWJH commented 4 years ago

This isn't a DI issue, closing.