Resolving MVC 5 Controllers with ctor parameters

29 views
Skip to first unread message

Michael Powell

unread,
Feb 26, 2016, 1:40:59 PM2/26/16
to Autofac
Hello,

I have the following:

ContainerBuilder builder = ... ;

// ...

builder.RegisterControllers(Assembly.GetExecutingAssembly());

Now, some of the controllers have ctor parameters, in proper DI manner.

However, I am receiving MissingMethodException for parameterless ctor in my AccountController.

Should I register AccountController, and others, by hand?

Not new to Autofac, but giving it a shot for performance reasons, as compared to, say, Ninject, which I have seen to be slower.

Does Autofac auto-resolve parameters? Or must I wire them up specific to the known ctor?

Thank you...

Sincerely,

Michael Powell

Travis Illig

unread,
Feb 27, 2016, 11:31:34 AM2/27/16
to Autofac
Autofac should autowire the constructors for you as long as you have all the dependencies also registered with the container. We have some docs with examples here: http://autofac.readthedocs.org/en/latest/integration/mvc.html

If you still have usage questions, we (and a LOT of others) monitor StackOverflow - just tag your question "autofac".

Michael Powell

unread,
Feb 27, 2016, 12:13:40 PM2/27/16
to aut...@googlegroups.com
On Sat, Feb 27, 2016 at 11:31 AM, Travis Illig <travis...@gmail.com> wrote:
> Autofac should autowire the constructors for you as long as you have all the dependencies also registered with the container. We have some docs with examples here: http://autofac.readthedocs.org/en/latest/integration/mvc.html
>
> If you still have usage questions, we (and a LOT of others) monitor StackOverflow - just tag your question "autofac".

Thanks. Been there; the key seems to be in SetResolver.

WithParameter doesn't hurt if there are any special cases, for
instance, but like you said, is probably unnecessary with sufficient
auto-wiring.

> --
> You received this message because you are subscribed to a topic in the Google Groups "Autofac" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/autofac/AzQfLyUYwlU/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to autofac+u...@googlegroups.com.
> To post to this group, send email to aut...@googlegroups.com.
> Visit this group at https://groups.google.com/group/autofac.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages