Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofac not finding the most greedy constructor

I'm trying to use Autofac to find the most greedy constructor in a referenced dll.

It's not finding it and only finds the one parameterless constructor.

These are the two ctors:

public SimpleAuthenticationController() { .. }

public SimpleAuthenticationController(IAuthenticationCallbackProvider callbackProvider) : this()

Now this is how I register the stuff with autofac:

var builder = new ContainerBuilder();

builder.RegisterType<SampleMvcAutoAuthenticationCallbackProvider>().As<IAuthenticationCallbackProvider>();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterControllers(typeof(SimpleAuthenticationController).Assembly);

var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

nothing too complex.

But this is the only weird thing I can think of.

  1. typeof(MvcApplication) is the same project where this code exists in, in global.asax
  2. typeof(MvcApplication) is found in a -seperate- dll, which I manually added via AddReferences.

Anyone see what I've done wrong?

like image 664
Pure.Krome Avatar asked Oct 22 '22 05:10

Pure.Krome


1 Answers

Problem was that my greedy was getting called, but if you looked at the greedy-ctor, you'll see that I'm doing : this().

This was a beginner error!

So it was calling the greedy ctor, but before it goes into scope, it has to bubble up to the other parameterless ctor. And I kept thinking it was skipping the greedy and just hitting the parameterless.

like image 85
Pure.Krome Avatar answered Oct 23 '22 21:10

Pure.Krome