ServiceStack - 使用Ninject代替Funq

时间:2013-02-28 11:24:39

标签: ninject servicestack

我正在尝试将ServiceStack与Ninject而不是Funq一起使用。我有以下内容:

public interface IContainerAdapter
{
    T Resolve<T>();
    T TryResolve<T>();
}

public class NinjectIocAdapter : IContainerAdapter
{
    private readonly IKernel kernel;

    public NinjectIocAdapter(IKernel kernel)
    {
        this.kernel = kernel;
    }

    public T Resolve<T>()
    {
        return this.kernel.Get<T>();
    }

    public T TryResolve<T>()
    {
        return this.kernel.TryGet<T>();
    }
}

然后在我的Configure方法中:

public override void Configure(Funq.Container container)
{
    //Set JSON web services to return idiomatic JSON camelCase properties
    ServiceStack.Text.JsConfig.EmitCamelCaseNames = true;

    IKernel kernel = new StandardKernel();
    container.Adapter = new NinjectIocAdapter(kernel);

    //Set MVC to use the same Funq IOC as ServiceStack
    //ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
}

但是我收到以下错误:

Cannot implicitly convert type 'NinjectIocAdapter' to 'ServiceStack.Configuration.IContainerAdapter'.

我也不确定是否必须取消注释该行才能将MVC设置为使用Funq IoC?我已经评论了它,因为我将使用Ninject。这是对的吗?

我认为一旦这一切都奏效了。我可以简单地在里面注册任何依赖项:

private static void RegisterServices(IKernel kernel)
{

}   

1 个答案:

答案 0 :(得分:3)

代码与the documentation完全匹配,但有一个细微差别。

您必须使用ServiceStack.Configuration.IContainerAdapter而不是自己的IContainerAdapter

删除您的实施,添加对ServiceStack.Configuration的引用,您应该没问题。