Unity: cannot register type from another assembly

I am trying to implement Dependency injection in my MVC application. I am using Unity.Mvc3.dll for IoC. I'm just wondering how Unity cannot register types from another assembly. Here is the code:

 protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); var container = new UnityContainer(); // ok: register type from this assembly container.RegisterType<IMessages, Messages>(); // fail: register type from another assembly (service reference) container.RegisterType<IPlayerService, PlayerService>(); DependencyResolver.SetResolver(new UnityDependencyResolver(container)); } public class UnityDependencyResolver : IDependencyResolver { private readonly IUnityContainer _container; public UnityDependencyResolver(IUnityContainer container) { this._container = container; } public object GetService(Type serviceType) { try { return _container.Resolve(serviceType); } catch (Exception ex) { throw ex; } } public IEnumerable<object> GetServices(Type serviceType) { try { return _container.ResolveAll(serviceType); } catch (Exception ex) { throw ex; } } } 

Use in MVC:

 [Dependency] public IPlayerService PlayerService { get; set; } [Dependency] public IMessages Messages { get; set; } public ActionResult Index() { PlayerMessageViewModel vm = new PlayerMessageViewModel(); vm.Messages = Messages; // success! vm.Players = PlayerService.Get(); // fail: null reference exception :PlayerService return View(vm); } 

PlayerService always zero as long as messages are in order.

Here is the PlayerService Class

 public class PlayerService : IPlayerService { private readonly MyDbEntities _dbContext; public PlayerService() { _dbContext = new MyDbEntities(); } public PlayerService(MyDbEntities dbContext) { _dbContext = dbContext; } public IQueryable<Player> Get() { return _dbContext.Players.AsQueryable(); } } 

this is a complete error message

Dependency resolution failed, type = "System.Web.Mvc.IControllerFactory", name = "(none)".
An exception occurred when: at resolution.
Exception: InvalidOperationException. The current type, System.Web.Mvc.IControllerFactory, is an interface and cannot be constructed. Do you miss type mapping?

+7
source share
4 answers

By default, the unit selects the constructor with the most parameters, so I think that it will try to use the second constructor with the dbContext parameter, which it cannot solve. If you mark your default constructor with the [InjectionConstructor] attribute, then I hope this solves your problem.

+6
source

You hide all exceptions with catch { return null; } catch { return null; } to remove it and see which exception is thrown at resolution. I think the exception is thrown in the PlayerService constructor.

EDIT:

From http://mvchosting.asphostcentral.com/post/ASPNET-MVC-3-Hosting-Problem-in-implementing-IControllerActivator-in-ASPNET-MVC-3.aspx

When the MVC application is launched for the first time, the resolver dependency is invoked with the following types in the following order:

  • IControllerFactory
  • IControllerActivator
  • Homecontroller

If you have not implemented your resolver to return null, if the type is not then you will probably end up with an error similar to:

The current type, System.Web.Mvc.IControllerFactory, is an interface and cannot be built

+7
source

You are violating the IDependencyResolver contract. It should return null for types that cannot be resolved. The problem with Unity is that it tries to build all the classes that it can find, regardless of whether they are registered in the container or not.

Consequently, a class cannot be built.

Nuget has the Unity.Mvc3 package.

+2
source

The current type (EX-DI.IBLL.IEmployee) is an interface and cannot be constructed. Do you miss type mapping?

Ans - Just in Unity.config.cs add the following thye

 public static class UnityConfig { public static void RegisterComponents() { var container = new UnityContainer(); // register all your components with the container here // it is NOT necessary to register your controllers **container.RegisterType<IEmployee,EmployeeManager>();** //this could be your interface and the class implementing the interface. DependencyResolver.SetResolver(new UnityDependencyResolver(container)); } } 
0
source

All Articles