如何配置Ninject以使用ServiceStack.net

ste*_*776 7 .net c# asp.net asp.net-mvc servicestack

在尝试配置ServiceStack.net以使用Ninject作为其IOC时,我收到的错误是指未定义的各种绑定.主要用于ICache客户端.

需要创建哪些特定绑定才能正确使用Ninject?

目前已指定:

Bind<ISessionFactory>().To<SessionFactory>();//Is this correct/needed?
Run Code Online (Sandbox Code Playgroud)

注意

我根据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);
}
Run Code Online (Sandbox Code Playgroud)

注3

我已经注册了ICacheClient,如下所示:Bind().To();

我现在收到指向IRequest的错误

Error activating IRequestLogger\nNo matching bindings are available, and the type is not self-bindable
Run Code Online (Sandbox Code Playgroud)

容器适配器

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>();
    }
}
Run Code Online (Sandbox Code Playgroud)

myt*_*thz 9

你有没有注入你的容器适配器:

container.Adapter = new NinjectIocAdapter(kernel);
Run Code Online (Sandbox Code Playgroud)

如果是这样,如果您还没有这样做,请尝试将您的AppHost类设为内部.应该只有1个AppHost实例,而一些IOC喜欢创建自己的实例,从第一个实例中删除所有配置.

你得到的行为听起来像Ninject抱怨未解决的依赖关系.通过kernal.TryGet<T>在容器适配器中使用,确保让Ninject通过Unresolved依赖项返回null ,例如:

public T TryResolve<T>()
{
    return this._kernel.TryGet<T>();
}
Run Code Online (Sandbox Code Playgroud)