Error activating, No matching bindings are available, and the type is not self-bindable

时间:2016-08-31 12:06:21

标签: asp.net-mvc ninject

Im getting an "ActivationException unhandled by user code" exception.

NinjectWebCommon.cs

 private static void RegisterServices(IKernel kernel)
    {
        var interfaceToImplementationMappings = new Dictionary<Type, Type>()
        {
            // Database layer
            {typeof(DataContext), typeof(DataContext)},

            {typeof(IParentNameRepository), typeof(ParentNameRepository)},
            {typeof(ICommentRepository), typeof(CommentRepository)},                
        };

        foreach (KeyValuePair<Type, Type> mapping in interfaceToImplementationMappings)
        {
            kernel.Bind(mapping.Key).To(mapping.Value).InRequestScope(); // For when injected during a request
            kernel.Bind(mapping.Key).To(mapping.Value).InBackgroundScope(); // For when injected into a background job
        }
    }

IParentNameRepository.cs

public interface IParentNameRepository : IGenericArchivableCRUDRepository<ParentName>
{
    ParentName GetByName(string name);
}

ParentNameRepository.cs

public class ParentNameRepository : GenericArchivableCRUDRepository<ParentName>, IParentNameRepository
{
    public ParentNameRepository(DataContext context) : base(context) { }

    public override void ValidateCommon(ParentName entity)
    {
        if (string.IsNullOrWhiteSpace(entity.Name))
            throw new DataValidationException("The Parent name cannot be empty");

        entity.Name = entity.Name.Trim().ToUpper();
    }

    public ParentName GetByName(string name)
    {
        return dbSet.Where(x => x.Name.Trim().Equals(name, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
    }
}

Im getting "Error activating IParentNameRepository No matching bindings are available, and the type is not self-bindable." error. I debug the code by putting the break point to CreateKernel() method and it not been hit.

0 个答案:

没有答案