Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing Unity Web API and MVC4 UnityDependencyResolvers: No parameterless constructor defined for this object

I am getting the following exception:

No parameterless constructor defined for this object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

when the framework attempts to construct controllers that do not derive from ApiController, such as my simple HomeController below:

 public class HomeController : Controller
        {
            private IUnityContainer _unityContainer;
            public HomeController(IUnityContainer unityContainer)
            {
                _unityContainer = unityContainer;
            }
            public ActionResult Index()
            {
                return View();
            }
        }

I have the following Boostrapper.cs class defined:

     public static class Boostrapper
        {
            public static void Initialize()
            {
                var container = BuildUnityContainer();
                GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);                          
            }

            private static IUnityContainer BuildUnityContainer()
            {
                var container = new UnityContainer();
                RegisterTypes(container);
                return container;
            }
        }

and Bootstrapper.Initialize() is the last line of code executed within Application_Start of Global.asax.cs?

Exception is not thrown on controller classes that derive from ApiController. Could someone please advise on what I am missing?

Thanks.

like image 624
Klaus Nji Avatar asked Dec 02 '25 05:12

Klaus Nji


1 Answers

Since I am mixing Web API and MVC4 within the same project, I need to also ensure that Unity.WebApi.UnityDependencyResolver and Unity.Mvc4.UnityDependencyResolver are both setup as indicated here.

My Boostrapper.cs which now looks like this:

    public static IUnityContainer Initialise()
    {
      var container = BuildUnityContainer();
      GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
      DependencyResolver.SetResolver(new UnityDependencyResolver(container));

      return container;
    }

 private static IUnityContainer BuildUnityContainer()
    {
      var container = new UnityContainer();
      RegisterTypes(container);
      return container;
    }

    public static void RegisterTypes(IUnityContainer container)
    {
        container.RegisterType<IAgencyResolver, AgencyByNameResolver>("ByName");
        container.RegisterType<IAgencyResolver, AgencyByCdeCodeResolver>("ByCode");
        container.RegisterType<IAgencyRepository, AgenciesRepository>();            
    }

allows both MVC and Web API controllers to be resolved successfully by Unity. To support both, I need NuGet packages Unity.Mvc4 and Unity.WebAPI. Hoping this does not bite me down the road.

like image 79
Klaus Nji Avatar answered Dec 03 '25 18:12

Klaus Nji



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!