JasperFx / lamar

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

bi-directional dependency while EntityFrameworkCore and AutoMapper are installed #254

Open OmerNassie opened 4 years ago

OmerNassie commented 4 years ago

I struggle understand the reason throwing this exception in my project. I searched all over the internet what can cause it while EntityFrameworkCore and AutoMapper are installed and using the AssembliesFromApplicationBaseDirectory() method when scanning for registries.

The full exception message: Detected some kind of bi-directional dependency while trying to discover and plan a missing service registration. Examining types: AutoMapper.TypePair, Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType

The project's dependencies:

    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
    <PackageReference Include="Lamar.Microsoft.DependencyInjection" Version="4.3.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyModel" Version="3.1.3" />
    <PackageReference Include="AspNetCore.HealthChecks.UI" Version="3.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="4.1.1" />
    <PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="3.1.3" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.3.3" />

my configure container method:

        public override void ConfigureContainer(ServiceRegistry registry)
        {
            registry.For<IConfiguration>().Use(Configuration).Singleton();
            registry.Scan(
                c =>
                {
                    c.WithDefaultConventions();
                    c.AssembliesFromApplicationBaseDirectory();
                    c.LookForRegistries();
                });
        }

.Net Core 3.1

jeremydmiller commented 3 years ago

That's not the full stack trace. Lamar tries to give you the whole path of how you made the circle.

And I very strongly recommend against this usage here:

        public override void ConfigureContainer(ServiceRegistry registry)
        {
            registry.For<IConfiguration>().Use(Configuration).Singleton();
            registry.Scan(
                c =>
                {
                    c.WithDefaultConventions();
                    c.AssembliesFromApplicationBaseDirectory();
                    c.LookForRegistries();
                });
        }

That's going to give you oodles of registrations you don't need. Use some kind of allow list to limit the assemblies you're scanning, and that might very well make this problem go away

OmerNassie commented 3 years ago

I managed to do a workaround for this because nothing I tried helped

            registry.For(typeof(Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType)).Use(typeof(Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType));
            registry.For<System.Security.Cryptography.X509Certificates.X509Certificate2>().Use<System.Security.Cryptography.X509Certificates.X509Certificate2>();
            registry.For<System.Security.Cryptography.X509Certificates.X509Certificate>().Use<System.Security.Cryptography.X509Certificates.X509Certificate>();

While investigating the subject, I've already done a whitelist as for your recommendation. Thank you