2

I am getting an error in my HelpController:

The constructor of type HelpController contains the parameter with name 'config' and type HttpConfiguration that is not registered. Please ensure HttpConfiguration is registered, or change the constructor of HelpController.

Is there any alternative to resolve this issue because I use Configuration to get Configuration.GetModelDescriptionGenerator();

public class HelpController : Controller
{
    private const string ErrorViewName = "Error";

    public HelpController(HttpConfiguration config)
    {
        Configuration = config;
    }

    public HttpConfiguration Configuration { get; private set; }

    ...

    public ActionResult ResourceModel(string modelName)
    {
        if (!String.IsNullOrEmpty(modelName))
        {
            ModelDescriptionGenerator modelDescriptionGenerator = Configuration.GetModelDescriptionGenerator();
            ModelDescription modelDescription;
            if (modelDescriptionGenerator.GeneratedModels.TryGetValue(modelName, out modelDescription))
            {
                return View(modelDescription);
            }
        }

        return View(ErrorViewName);
    }
}
CodyF
  • 4,977
  • 3
  • 26
  • 38
Aftab Ahmed
  • 665
  • 9
  • 17
  • Have you tried registering the `HttpConfiguration`? – Steven Feb 01 '17 at 11:30
  • @Steven how can i register it with-out interface? – Aftab Ahmed Feb 01 '17 at 11:56
  • 1
    I think I don't really understand your question. You just register it. Try `container.RegisterSingleton(config);`. – Steven Feb 01 '17 at 12:06
  • than @Steven it worked – Aftab Ahmed Feb 01 '17 at 12:37
  • Did you get this suggestion from @Steven working? I don't have `config` defined in my Global.asax and if I try just `container.RegisterSingleton()` I get `For the container to be able to create HttpConfiguration it should have only one public constructor: it has 2` – Caltor Mar 29 '17 at 08:48
  • Interestingly I have just found that if I remove / comment out the line `container.RegisterMvcControllers(Assembly.GetExecutingAssembly());` in my Global.asax but keep `container.RegisterWebApiControllers(GlobalConfiguration.Configuration);` my (MVC) Controllers still get their dependencies injected but the HelpController still works as before. I've had to use a Hybrid LifeStyle though otherwise you get lifestyle errors at runtime presumably due to a mismatch between WebAPI and MVC. – Caltor Mar 29 '17 at 09:02
  • @Caltor: If you're mixing Web API and MVC in the same project (which means you always run it inside IIS), you should typically only use `WebRequestLifestyle` as scoped lifestyle. There should be no need for a hybrid lifestyle. – Steven Mar 29 '17 at 09:08
  • @Caltor: You should _not_ remove the call to `RegisterMvcControllers`, because that would blind Simple Injector's verification and diagnostics system. – Steven Mar 29 '17 at 09:10
  • @Steven OK I've put the `RegisterMvcControllers` back in. I was able to resolve this problem by changing the constructor above to private (thanks to a suggestion from a colleague). The OP original code seems to omit the parameterless constructor that I have in my VS generated code which makes this work. I'll post the fixed code as an answer. – Caltor Mar 29 '17 at 09:21

1 Answers1

2

Change your constructor to private and put back in the parameterless constructor as generated by VS. Your HelpController should look like this.

public class HelpController : Controller
{
    private const string ErrorViewName = "Error";

    public HelpController()
        : this(GlobalConfiguration.Configuration)
    {
    }

    private HelpController(HttpConfiguration config)
    {
        Configuration = config;
    }


    public HttpConfiguration Configuration { get; private set; }
}
Caltor
  • 2,538
  • 1
  • 27
  • 55