AutoFac does not register api controller

I am using AutoFac for a registered api controller but don't know what the problem is

Getting error

Type 'myApp.WebAPI.Controllers.myController' does not have a default constructor 

I added the IocHelper class below

 public class IocHelper { public static IContainer CreateContainer() { var builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container); return container; } } 

Thanks in advance.

0
asp.net-web-api autofac repository-pattern
Jun 09 '15 at 13:58
source share
1 answer
  Please refer the below solution, and also make sure you declared the injected interfaces as public explicitly. //Global.asax file -Application Start method protected void Application_Start() { var bootStrapApp = new BootstrapApplication() .ConfigureRoute(GlobalConfiguration.Configuration) .ConfigureContainer(new ContainerBuilder(), GlobalConfiguration.Configuration); } // BootstrapApplication.cs class file public class BootstrapApplication { public BootstrapApplication ConfigureRoute(HttpConfiguration httpConfiguration) { httpConfiguration.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional }); return this; } public BootstrapApplication ConfigureContainer(ContainerBuilder container, HttpConfiguration httpConfiguration) { var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(n => n.FullName.StartsWith("<projectNamespacevalue>")).ToArray(); //projectNamespacevalue for ex. AspNet.WebApi2.Demo // var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(n => n.FullName.StartsWith("AspNet")).ToArray(); container.RegisterAssemblyTypes(assemblies).AsImplementedInterfaces(); container.RegisterApiControllers(assemblies); //WebApi controllers scanning and registration var ioccontainer = container.Build(); httpConfiguration.DependencyResolver = new AutofacWebApiDependencyResolver(ioccontainer); return this; } } 
0
Oct 27 '16 at 6:17
source share



All Articles