Ninject 2.1 ActivationException:激活字符串时出错

我很困惑为什么我收到“Ninject.ActivationException:错误激活字符串没有匹配的绑定可用,并且类型不可自绑定”在随机绑定中。 如果我保留IMedia的绑定,它将抛出ActivationException,但是如果我使用CallbackProvider它就可以工作。 所有这些类的结构都相同,只有几个不同的属性。 我很困惑为什么ILocationType,IMedia和IFarmDataContext抛出ActivationException而其他人没有。 有任何想法吗?

/****************************** * Core Types ******************************/ Bind().ToProvider(new CallbackProvider(delegate { return new FarmDataContext(); })); //Media Bind().To(); //blows up //Bind().ToProvider(new CallbackProvider(delegate { return new Media(); })); Bind().To(); Bind().To(); //Location Bind().To(); Bind().ToProvider(new CallbackProvider(delegate { return new LocationType(); })); Bind().To(); 

Ninject没有绑定“String key”来注入Media .ctor; 当它尝试创建依赖于Media的类型时,它不知道如何解决依赖关系和抛出。 对于大多数类型,Ninject会尝试为您创建一些东西,但是字符串和值类型不可自我绑定,因为我们没有良好的默认值,它可能会对使用不同约定的基元类型造成严重破坏。

您需要在绑定中添加参数值(.WithContructorArgument(“key”,someValue))或使用某种提供程序(您已经完成)。

以下是IMedia界面和媒体实现。 Media是一个部分类,其主要类是通过LinqToSql DBML文件生成的。 上面列出的绑定列表中的所有类型都是这种情况。

 public interface IMedia : IValidationDictionary, IBaseDescriptor { ///  /// Returns a specific Media object specifying /// if you want the full or lite version ///  ///  ///  ///  ///  IMedia Get(long id, bool isLite, DataContext context); ///  /// Returns the lite version of the request Media object ///  ///  ///  ///  IMedia Get(long id, DataContext context); ///  /// Returns a list of Media Objects ///  ///  ///  ///  ValidationList List(bool isLite, DataContext context); ///  /// Returns a list of Media Objects with pagination capabilities ///  ///  ///  ///  ///  ///  ValidationList List(bool isLite, int skip, int top, DataContext context); } public partial class Media : BaseDescriptor, IMedia { #region Constructors public Media(String key, IError error) : base() { AddError(key, error); } #endregion #region Properties public MediaType Type { set { if (TypeID <= 0) { MediaType = value; } } get { return MediaType; } } #endregion #region Internal Methods ///  /// Truncates relationships as appropriate to reduce over-the-wire size ///  protected override void MakeLite() { MediaRelateds = new EntitySet(); } ///  /// Validates the values and returns true if there are no problems. ///  override public bool Validate() { this.ClearErrors(); if (this.TypeID <= 0) { this.AddError("TypeID", new Error(ErrorType.VALIDATION, "TypeID is missing or invalid")); } if (string.IsNullOrEmpty(this.Path)) { this.AddError("Path", new Error(ErrorType.VALIDATION, "Path is missing or invalid")); } if (this.CreatedOn.Year <= 1900) { this.AddError("CreatedOn", new Error(ErrorType.VALIDATION, "CreatedOn is missing or invalid")); } if (this.CreatedBy <= 0) { this.AddError("CreatedBy", new Error(ErrorType.VALIDATION, "CreatedBy is missing or invalid")); } if (this.UpdatedOn.Year <= 1900) { this.AddError("UpdatedOn", new Error(ErrorType.VALIDATION, "UpdatedOn is missing or invalid")); } if (this.UpdatedBy <= 0) { this.AddError("UpdatedBy", new Error(ErrorType.VALIDATION, "UpdatedBy is missing or invalid")); } if (!string.IsNullOrEmpty(this.Path) && this.Path.Length > 255) { this.AddError("Path", new Error(ErrorType.VALIDATION, "Path is longer than the maximum of 255 characters")); } return (this.ErrorCount == 0); } #endregion #region Public Methods public ValidationList List(bool isLite, DataContext context) { return List(isLite, 0, 0, context); } public ValidationList List(bool isLite, int skip, int top, DataContext context) { if (context == null) { context = new DataContext(); } var query = context.Medias.Where(x => x.DeletedOn == null); List results; if (skip > 0 || top > 0) { if (top > 0) { if (skip < 0) { skip = 0; } results = query.OrderBy(x => x.ID).Skip(skip).Take(top).ToList(); } else { results = query.OrderBy(x => x.ID).Skip(skip).ToList(); } } else { results = query.OrderBy(x => x.ID).ToList(); } var finalResult = new ValidationList(new List()); foreach (var result in results) { result.IsLite = isLite; finalResult.Source.Add(result); } return finalResult; } public IMedia Get(long id, DataContext context) { return Get(id, false, context); } public IMedia Get(long id, bool isLite, DataContext context) { if (context == null) { context = new DataContext(); } var results = context.Medias.Where(x => x.ID == id && x.DeletedOn == null).ToList(); var result = (results.Count > 0 ? results[0] : null); if (result != null) { result.IsLite = isLite; } return result; } #endregion }