Ninject 2.1 ActivationException:激活字符串时出错
我很困惑为什么在随机绑定中收到“Ninject.ActivationException:错误激活字符串没有可用的匹配绑定,并且类型不可自绑定”。如果我保留 IMedia 的绑定,它将抛出 ActivationException,但如果我使用 CallbackProvider,它就会起作用。所有这些类的结构都相同,但具有一些不同的属性。我很困惑为什么 ILocationType、IMedia 和 IFarmDataContext 会抛出 ActivationException 而其他的则不会。有什么想法吗?
/******************************
* 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>();
I am confused about why I am receiving "Ninject.ActivationException : Error Activating string No matching bindings are available, and the type is not self-bindable" in random bindings. If I leave the binding for IMedia in place it will throw the ActivationException, but if I use the CallbackProvider it works. All of these classes are structured the same with a few different properties. I'm confused as to why ILocationType, IMedia, and IFarmDataContext throw ActivationException while the 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>();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Ninject 没有绑定要在 Media .ctor 中注入的“String key”;当它尝试创建依赖于 Media 的类型时,它不知道如何解决依赖关系并抛出异常。对于大多数类型,Ninject 会尝试为您创建一些东西,但是字符串和值类型不能自绑定,因为我们没有为它们提供良好的默认值,并且它可能会对使用不同基元约定的类型造成严重破坏。
您需要在绑定中添加参数值(.WithContructorArgument("key", someValue))或使用某种提供程序(您已经完成)。
Ninject doesn't have a binding for the "String key" to inject in the Media .ctor; When it tries to create a type that depends on Media, it doesn't know how to resolve the dependency and throws. For most types, Ninject would try to create something for you, but string and value types are not self-bindable as we don't have a good default value for them and it can cause havoc on types that use different conventions with primitives.
You need add a parameter value in your bindings (.WithContructorArgument("key", someValue)) or use some kind of provider (which you have done).
下面是 IMedia 接口和 Media 实现。 Media 是一个分部类,其主类是通过 LinqToSql DBML 文件生成的。上面绑定列表中列出的所有类型都是这种情况。
Below are the IMedia interface and Media implementation. Media is a partial class with the primary class generated via a LinqToSql DBML file. This is the case for all of the types listed above in the list of bindings.