Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am writing a mvc 4 application and using Ninject for dependency injection. The dependency injection class as given below

public class NinjetDependencyResolver : IDependencyResolver
{
    private IKernel kernel;

    public NinjetDependencyResolver()
    {
        kernel = new StandardKernel();
        this.AddBindings();
    }

    private void AddBindings()
    {
        //Add binding here
        kernel.Bind<IHRRepository>().To<HRRepository>();
    }

    public object GetService(Type serviceType)
    {
        return kernel.TryGet(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return kernel.GetAll(serviceType);
    }
}


this class is working ok with the Controllers. But I have created a class as given below

public class RoleVerfier
     {
       private IHRRepository repository;

       public RoleVerfier(_repository)
       {

           this.repository = _repository;
       }

       //some code here for funcionality
   }


and when I try to create a object of the RoleVerifier class as given below

try
{
       RoleVerfier verifier = new RoleVerfier();
                            
       //some code here for funcionality
                           
}
catch (Exception ex)
{

}


It is giving a Compile time error because the RoleVerifier Class constructor is asking to supply IHRRepository interface. But i think IHRRepository interface should be automatically provided by the Ninject as it is doing for the Controller classes.


I do not know what is the issue?

What I have tried:

Please help me as soon as possible
Posted
Updated 22-Mar-17 6:40am
Comments
Maciej Los 22-Mar-17 11:59am    
What is exact error message? Please, improve your question and provide that message.

1 solution

Your code is create a new instance of the class so you need to obey the constructor rules. There is nothing in your code that is using Ninject so you're not doing dependency resolving. What you have to do is use the DependencyResolver to get the instance of your class

c# - Asp.net mvc 4 dependency resolver - Stack Overflow[^]

If your ninject resolver is registered as a dependency resolver then using DependencyResolver as per the link above will pass the object creation to ninject and the constructor will be injected for you by ninject. The reason your code works for controllers is because when the mvc framework creates an instance of your controller it uses the DependencyResolver to do so. You can't just create classes like normal using "new" and expect them to be routed through ninject.
 
Share this answer
 
Comments
irfankhan200 22-Mar-17 13:01pm    
I have configured the Dependency resolver
Here is the code

public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

DependencyResolver.SetResolver(new NinjetDependencyResolver());

WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
}


And it is working for the Controllers here is the code how i Am using it in controller

public class DepartmentController : Controller
{
//
// GET: /Department/
private IHRRepository repository;
public DepartmentController(IHRRepository rep)
{
this.repository = rep;
}
public ActionResult Index()
{
try
{
Models.DepartmentModel model = new Models.DepartmentModel();
model.departmentsList = this.repository.Departments;
model.department = new Entities.Department();
ViewBag.OperationName = "Add Department";

return View(model);
}
catch (Exception ex)
{
return View("Error");
}
}
}
F-ES Sitecore 23-Mar-17 5:15am    
So use DependencyResolver.Current.GetService to get an instance of the class.
irfankhan200 23-Mar-17 6:50am    
Thanks it worked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900