在尝试配置 ServiceStack.net 以使用 Ninject 作为其 IOC 时,我收到有关未定义各种绑定(bind)的错误。主要用于 ICache 客户端。
需要创建哪些特定绑定(bind)才能正确使用 Ninject?
目前已指定:
Bind<ISessionFactory>().To<SessionFactory>();//Is this correct/needed?
注意
我已经根据 ServiceStack 文档创建了一个 IContainerAdapter 来实现 Ninject 的使用。 (在这里找到:ServiceStack IOC Docs)
注2 我的 apphost 配置方法如下所示:
public override void Configure(Funq.Container container)
{
IKernel kernel = new StandardKernel(new BindingModule());
container.Adapter = new NinjectContainerAdapter(kernel);
}
注3
我注册了 ICacheClient 如下: 绑定(bind)().To();
我现在收到指向 IRequest 的错误
Error activating IRequestLogger\nNo matching bindings are available, and the type is not self-bindable
容器适配器
public class NinjectContainerAdapter : IContainerAdapter
{
private readonly IKernel _kernel;
public NinjectContainerAdapter(IKernel kernel)
{
this._kernel = kernel;
}
public T TryResolve<T>()
{
return this._kernel.Get<T>();
}
public T Resolve<T>()
{
return this._kernel.Get<T>();
}
}
最佳答案
您是否已将容器适配器注入(inject):
container.Adapter = new NinjectIocAdapter(kernel);
如果是这样,请尝试将您的 AppHost 类设置为内部(如果您还没有这样做的话)。应该只有 1 个 AppHost 实例,一些 IOC 喜欢创建自己的实例,从第一个实例中删除所有配置。
您得到的行为听起来像是 Ninject 提示 Unresolved 依赖关系。使用 kernal.TryGet<T>
确保让 Ninject 返回带有未解析依赖项的 null在您的容器适配器中,例如:
public T TryResolve<T>()
{
return this._kernel.TryGet<T>();
}
关于c# - 如何配置 Ninject 与 ServiceStack.net 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9518952/