2

I searched a lot to solve the problem but no solution worked and I am new to DI and Ninject.

My problem is that i can get my service in WebUi layer but i can not get my service in Business layer.

This code work in my WebUi but it doesnt work in my business layer:

public IValidator<T> GetValidator<T>(T entity)
{
    var d = kernel.Get<IValidator<T>>();
    return d;
}

I get this error in business layer:

Error activating IValidator{Type} No matching bindings are available, and the type is not self-bindable. Activation path: 1) Request for IValidator{Type}

Suggestions: 1) Ensure that you have defined a binding for IValidator{Type}. 2) If the binding was defined in a module, ensure that the module has been loaded into the kernel. 3) Ensure you have not accidentally created more than one kernel. 4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name. 5) If you are using automatic module loading, ensure the search path and filters are correct.

This is my code:

My interface:

public interface IValidationService
{
    ValidationState Validate<T>(T model);
}

My concrete class:

public class ValidationService : IValidationService
{
    private readonly IKernel kernel;
    public ValidationService(IKernel kernel)
    {
        this.kernel = kernel;
    }

    public ValidationState Validate<T>(T model)
    {
        var validator = kernel.Get<IValidator<T>>();
        if (validator == null) // or just return null?
            throw new Exception(string.Format("No validator found for type                 ({0})", model.GetType()));

        return validator.Validate(model);
    }

}

and my binding:

kernel.Bind<IValidationService>().To<ValidationService>();
Mashtani
  • 621
  • 11
  • 24

1 Answers1

1

After a long time inspecting, I got the problem.

I have done this and my problem gone:

kernel.Bind<IValidationService>().To<ValidationService>()
            .WithConstructorArgument(typeof(IKernel));
Mashtani
  • 621
  • 11
  • 24