Simple Injector - No parameterless constructor defined for this object

荒凉一梦 提交于 2019-12-05 01:31:23

The reason for getting this error is because you are missing the following registration (as explained in the MVC integration guide):

DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));

MVC and Web API both have their own abstraction for resolving dependencies (both called 'dependency resolver'). Since you didn't set the resolver for MVC, MVC uses the default resolution mechanism for creating MVC controllers, but this requires controllers to have a default constructor.

Calling DependencyResolver.SetResolver will solve the problem.

For Web API Projects, the SetResolver method described above will compile but when the application is run, you'll get an ArgumentException error: "Additional information: The type SimpleInjector.Integration.WebApi.SimpleInjectorWebApiDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator."

You'll resolved this issue by setting the DependencyResolver to a SimpleInjectorWebApiDependencyResolver and pass in your container, like below.

GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!