1

I'm using the following code for registering all of my controllers:

protected override void Load(ContainerBuilder builder)
{
  builder.RegisterControllers(typeof(MvcApplication).Assembly)
           .OnActivated(a => a.Context.InjectUnsetProperties(a.Instance))
           .OnActivating(InjectInvoker);
}
private static void InjectInvoker(IActivatingEventArgs<object> obj)
{
    var invoker = obj.Context.ResolveOptional<IActionInvoker>();
    if (invoker != null)
    {
        ((Controller)obj.Instance).ActionInvoker = invoker;
    }
}

but now I have a controller that takes 2 interfaces of the same kind:

        builder.Register(c => new RestClient(Url1))
            .Named<IRestClient>("Url1")
           .InstancePerLifetimeScope();

        builder.Register(c => new RestClient(Url2))
            .Named<IRestClient>("Url2")
           .InstancePerLifetimeScope();

    public HomeController(IRestClient r1, IRestClient r2)

I get the following error: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Site.Controllers.HomeController' can be invoked with the available services and parameters: Cannot resolve parameter 'RestSharp.IRestClient r1' of constructor 'Void .ctor(RestSharp.IRestClient, RestSharp.IRestClient)'. Which I did expect, so then I've tried adding the following code:

        builder.Register(
            c =>
            new HomeController(
                               c.ResolveNamed<IRestClient>("Url1"),
                               c.ResolveNamed<IRestClient>("Url2")))
                             .As<IController>()
                             .InstancePerLifetimeScope();

which then I got the same error again.

so what is the correct way to register a controller in the kind of situation? thanks.

5
  • Two things your custom controller registration should go after the RegisterControllers call and try it without the .As<IController>() part Commented Jun 26, 2013 at 15:24
  • Where do Url1 and Url2 come from, are these variables? Commented Jun 26, 2013 at 15:25
  • the code is an example...the variables are set from the config file, so it would be Properties.Settings.Default.Url1 Commented Jun 26, 2013 at 15:31
  • nemesv: yes that worked:) you should post that as an answer. also any insight on why that works would be appreciated. Commented Jun 26, 2013 at 15:35
  • @user261490 I've added it as an answer with some additional explanation. Commented Jun 26, 2013 at 17:07

1 Answer 1

1

You need to register your controller without the .As<IController>() (or you need to use .AsSelf() but this is default setting).

You need to this because the framework will try to resolve your Controller with using the concrete type HomeController and not the IController interface (so the RegisterControllers method register all the controller with .AsSelf() internally).

So the following registration should work

builder.Register(
    c =>
        new HomeController(
            c.ResolveNamed<IRestClient>("Url1"),
            c.ResolveNamed<IRestClient>("Url2")))
        .InstancePerLifetimeScope();

Note: you should have this call after the RegisterControllers call.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.