Question

I'm using Autofac and inject all regtries of an interface for some reason this must be lazy. But I got double entries in this list. There are 3 plugins but my ienum contains 6. Every plugin is injected twice. I could use Distinct, but this seems to be a hack.

public WorkInProgressService(IEnumerable<Lazy<IWorkInProgressPlugin>> plugins)
{
  this.plugins = plugins;
}

I checked and it is definetly not registered twice.

Was it helpful?

Solution

Without seeing your code, I'm guessing that you registered each one twice.

You can get a little more info about the registrations if you put run this code somewhere that you have access to your container and examine the objects in the array:

var reg = container.ComponentRegistry
            .RegistrationsFor(new TypedService(typeof (IWorkInProgressPlugin)))
            .ToArray();

If you're getting 6 plugins in your parameter, you'll get 6 registration entries.

Autofac's collection support is provided by this class. It gets every registration matching the service element, resolves each one, and puts them into an array.

Autofac's Lazy<> support is provided by this class. It takes all registrations of the inner service and for each one, creates a new registration that wraps it in a Lazy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top