Trying to use IoC for asp.net mvc 4, the exception "Without parameters without constructor for this object"

I am trying to configure IoC using StructureMap for my ASP.NET MVC 4 site. Here is my StructureMapDependencyResolver class:

  public class StructrueMapDependencyResolver : IDependencyResolver { public object GetService(Type serviceType) { return IocContainer.GetInstance(serviceType); } public IEnumerable<object> GetServices(Type serviceType) { return IocContainer.GetAllInstances(serviceType); } } 

Here is a part of my IocContainer

 public static class IocContainer { .... public static object GetInstance(Type t) { return ObjectFactory.TryGetInstance(t); } public static IEnumerable<object> GetAllInstances(Type t) { return ObjectFactory.GetAllInstances(t).Cast<object>(); } } 

Here is what my Global.aspx.cs looks like

 public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); IocContainer.RegisterAllTypes(Server.MapPath("~\\Bin"), AssemblyList.MyAssemblies); DependencyResolver.SetResolver(new StructrueMapDependencyResolver()); } } 

Finally, I have a simple controller that depends on the service:

  public class ManagePostController: Controller { private ISomeService _someService; public ManagePostController(ISomeService svc) { _someService= svc; } } 

When I launched my site, I received the following exception:

There is no constructor without parameters for this object.

[InvalidOperationException: An error occurred while trying to create a controller of type "Foothill.WebAdmin.Controllers.ManagePostController". Make sure the controller has an immortal public constructor.] System.Web.Mvc.DefaultControllerActivator.Create (RequestContext requestContext, Type controllerType) +247

I'm not sure where I need to change?

+4
source share
2 answers

Replace the implementation of IocContainer.GetInstance(Type) as follows:

 public static object GetInstance(Type t) { return t.IsAbstract || t.IsInterface ? ObjectFactory.TryGetInstance(t) : ObjectFactory.GetInstance(t); } 

The ASP.NET MVC extensibility model will attempt to resolve various components (e.g. IControllerActivator ) that are optional (if you return null , MVC will use the default components). Therefore, we have a call to ObjectFactory.TryGetInstance - this will only allow the component if you explicitly configure it in the container.

To enable controllers (which are specific types), you should use ObjectFactory.GetInstance - this creates an instance, even if the controller type has never been explicitly configured.

The code snippet above is what I use in my projects, and I just realized that it is very similar to what is present in the Native Pack Framework MVC4 (see line 123 in this file ).

By the way, I think you could just use the NuGet package instead of doing these steps yourself.

Update

Regarding IControllerActivator : this is just another extensibility point. If the controller activator is not registered, the dependency converter is used instead:

If there is no IControllerActivator in the dependency converter, we will then ask the dependent converter to create a specific controller type by calling GetService (controllerType). (quote from Brad Wilson's blog )

It also explains why IDependencyResolver and IControllerActivator : http://forums.asp.net/post/4241343.aspx

+4
source

I had the same problem using the IoC container for several hours, but I could not get rid of it. Finally, I install the latest Visual Studio update (version VS 2013 and update3 in my case) and the problem will be solved.

0
source

Source: https://habr.com/ru/post/1496434/


All Articles