I am trying to setup 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 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 a service:
public class ManagePostController: Controller
{
private ISomeService _someService;
public ManagePostController(ISomeService svc)
{
_someService= svc;
}
}
When I start my website, got the following exception:
No parameterless constructor defined for this object.
[InvalidOperationException: An error occurred when trying to create a controller of type 'Foothill.WebAdmin.Controllers.ManagePostController'. Make sure that the controller has a parameterless public constructor.] System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +247
I am not sure where I need to change?