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?
source share