Ninject 2.1 ActivationException: Error activating string

I am confused about why I get "Ninject.ActivationException: Error String Activation There are no bindings available, and the type is not self-binding" in random bindings. If I leave the IMedia binding in place, it will throw an ActivationException, but if I use CallbackProvider, it will work. All of these classes are structured identically with several different properties. I am confused why ILocationType, IMedia and IFarmDataContext raise an ActivationException, while others do not. Any ideas?

/****************************** * Core Types ******************************/ Bind<IFarmDataContext>().ToProvider(new CallbackProvider<IFarmDataContext>(delegate { return new FarmDataContext(); })); //Media Bind<IMedia>().To<Media>(); //blows up //Bind<IMedia>().ToProvider(new CallbackProvider<IMedia>(delegate { return new Media(); })); Bind<IMediaType>().To<MediaType>(); Bind<IMediaRelated>().To<MediaRelated>(); //Location Bind<ILocation>().To<Location>(); Bind<ILocationType>().ToProvider(new CallbackProvider<ILocationType>(delegate { return new LocationType(); })); Bind<ILocationDetail>().To<LocationDetail>(); 
+7
c # inversion-of-control ninject
source share
2 answers

Ninject does not have a binding for the "String key" for injection into Media.ctor; When he tries to create a type that depends on the media, he does not know how to resolve dependencies and throws. For most types, Ninject will try to create something for you, but the types of strings and values ​​are not self-switching, since we do not have a good default value for them, and this can lead to chaos of types that use different conventions with primitives.

You need to add the parameter value to your bindings (.WithContructorArgument ("key", someValue)) or use some kind of provider (which you did).

+4
source share

The following are the IMedia interface and multimedia implementation. Media is a partial class with the main class generated through the LinqToSql DBML file. This applies to all the types listed above in the list of bindings.

 public interface IMedia : IValidationDictionary, IBaseDescriptor { /// <summary> /// Returns a specific Media object specifying /// if you want the full or lite version /// </summary> /// <param name="id"></param> /// <param name="isLite"></param> /// <param name="context"></param> /// <returns></returns> IMedia Get(long id, bool isLite, DataContext context); /// <summary> /// Returns the lite version of the request Media object /// </summary> /// <param name="id"></param> /// <param name="context"></param> /// <returns></returns> IMedia Get(long id, DataContext context); /// <summary> /// Returns a list of Media Objects /// </summary> /// <param name="isLite"></param> /// <param name="context"></param> /// <returns></returns> ValidationList<IMedia> List(bool isLite, DataContext context); /// <summary> /// Returns a list of Media Objects with pagination capabilities /// </summary> /// <param name="isLite"></param> /// <param name="skip"></param> /// <param name="top"></param> /// <param name="context"></param> /// <returns></returns> ValidationList<IMedia> 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 /// <summary> /// Truncates relationships as appropriate to reduce over-the-wire size /// </summary> protected override void MakeLite() { MediaRelateds = new EntitySet<MediaRelated>(); } /// <summary> /// Validates the values and returns true if there are no problems. /// </summary> 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<IMedia> List(bool isLite, DataContext context) { return List(isLite, 0, 0, context); } public ValidationList<IMedia> 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<Media> 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<IMedia>(new List<IMedia>()); 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 } 
0
source share

All Articles