Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Ninject to work with ServiceStack.net

In trying to configure ServiceStack.net to use Ninject as its IOC, I am getting errors referring to various bindings not being defined. Primarily for ICache Client.

What specific bindings need to be created to use Ninject properly?

Currently have specified:

Bind<ISessionFactory>().To<SessionFactory>();//Is this correct/needed?

Note

I have created an IContainerAdapter as per the ServiceStack documention to implement the use of Ninject. (Found here:ServiceStack IOC Docs)

Note 2 My apphost configure method looks like this:

public override void Configure(Funq.Container container)
{
        IKernel kernel = new StandardKernel(new BindingModule());
        container.Adapter = new NinjectContainerAdapter(kernel);
}

Note 3

I have registered the ICacheClient as follows: Bind().To();

And I am now getting an error pointing to IRequest

Error activating IRequestLogger\nNo matching bindings are available, and the type is not self-bindable

Container Adapter

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>();
    }
}
like image 547
stephen776 Avatar asked Mar 01 '12 15:03

stephen776


2 Answers

Have you injected your Container adapter with:

container.Adapter = new NinjectIocAdapter(kernel);

If so, try also make your AppHost class internal if you haven't done so. There should only be 1 instance of AppHost and some IOC's like to create their own instance, wiping out all the configuration from the first one.

The behavior you're getting sounds like Ninject is complaining about unresolved dependencies. Make sure you get Ninject to return null with Unresolved dependencies by using kernal.TryGet<T> in your Container Adapter, e.g:

public T TryResolve<T>()
{
    return this._kernel.TryGet<T>();
}
like image 64
mythz Avatar answered Nov 13 '22 18:11

mythz


You need to write your own IContainerAdapter and then set Container.Adapter in your AppHost

like image 1
Jon Canning Avatar answered Nov 13 '22 20:11

Jon Canning