JasperFx / lamar

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

Using .AddHttpClient(..) in a child-ServiceRegistry results in logging added twice #326

Closed dougbenham closed 2 years ago

dougbenham commented 2 years ago
var registry = new Registry();
registry.AddLogging();
registry.IncludeRegistry<OtherRegistry>();
class OtherRegistry : ServiceRegistry
{
   public OtherRegistry()
   {
      this.AddHttpClient(...);
   }
}

Registrations around logging end up getting messed up because many things are added twice. Is there a workaround for this?

dougbenham commented 2 years ago

I could just make some extension method like this instead:

static class OtherRegistryExtensions 
{
   public static ServiceRegistry AddOther(this ServiceRegistry services)
   {
      services.AddHttpClient(...);
      return services;
   }
}

But then we aren't using the child ServiceRegistry logic anymore and instead are just continuing to work with the primary ServiceRegistry. What are the benefits of using child ServiceRegistries?

jeremydmiller commented 2 years ago

@dougbenham How is logging getting added twice? Is AddHttpClient() quietly doing a second AddLogging() somehow? I'm not following exactly why this would be a Lamar error.

dougbenham commented 2 years ago

@dougbenham How is logging getting added twice? Is AddHttpClient() quietly doing a second AddLogging() somehow? I'm not following exactly why this would be a Lamar error.

Yes you can see the source here: image

And then in Lamar, you can see how it just naively appends all of the registrations: image

This results in logging being added twice. Anyways I don't see a way around this, it just kinda ruins the .IncludeRegistry(..) methods for any practical purposes. Forced to work with the primary service registry at all times so that you don't encounter weird issues like this.

jeremydmiller commented 2 years ago

Okay. There's nothing that's really actionable here, so I'm closing the issue. Just don't call the AddHttpClient() from an additional Lamar ServiceRegistry is all I can say here.