我正在尝试将 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>();
}
}
然后在我的配置方法中:
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)
{
}