0

In App_Start

IContainer container = SetupDependencyInjection();

GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

and

 private IContainer SetupDependencyInjection()
    {
        var builder = new ContainerBuilder();
        builder.RegisterApiControllers(typeof(WebApiApplication).Assembly);
    builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
        IContainer container = builder.Build();
        return container;
    }

This is working good but sometimes it shows error Error occurred when trying to create a controller of type 'ControllerName'. Make sure that the controller has a parameterless public constructor.

i am new to Web Api. Please help me.

  • I would assume that this is a result of your IoC container not being able to resolve dependencies for `ControllerName`, normally due to lack of registration of the resolved types. Make sure parameters `ControllerName` ctor expects are registered in your IoC container. – Michael Nov 17 '16 at 10:37
  • Have you searched for an answer using the error message? This is a really common question. – Travis Illig Nov 18 '16 at 14:08
  • Possible duplicate of [None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'](http://stackoverflow.com/questions/38927507/none-of-the-constructors-found-with-autofac-core-activators-reflection-defaultc) – Travis Illig Nov 18 '16 at 14:15
  • Potential duplicate of: http://stackoverflow.com/q/38927507/8116 , http://stackoverflow.com/q/39561779/8116 , http://stackoverflow.com/q/40005815/8116 , http://stackoverflow.com/q/40341738/8116 – Travis Illig Nov 18 '16 at 14:16

0 Answers0