Ninject 2.1 ActivationException:激活字符串时出错 [英] Ninject 2.1 ActivationException : Error activating string

查看:84
本文介绍了Ninject 2.1 ActivationException:激活字符串时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对为什么我在随机绑定中收到"Ninject.ActivationException:错误激活字符串,没有可用的匹配绑定,并且类型不可自绑定"感到困惑.如果我将IMedia的绑定保留在适当的位置,它将抛出ActivationException,但是如果我使用CallbackProvider,它将起作用.所有这些类的结构均相同,但具有一些不同的属性.我对为什么ILocationType,IMedia和IFarmDataContext抛出ActivationException而其他人却不抛出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>();

解决方案

Ninject对要插入到Media .ctor中的字符串键"没有绑定.当它尝试创建依赖于Media的类型时,它不知道如何解决依赖关系并引发异常.对于大多数类型,Ninject会尝试为您创建一些内容,但是字符串和值类型不是可自绑定的,因为我们没有很好的默认值,这可能导致对使用不同原语约定的类型造成破坏. /p>

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

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>();

解决方案

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).

这篇关于Ninject 2.1 ActivationException:激活字符串时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆