Sharing Container with ServiceStack container

May 16, 2012 at 7:04 AM

Hi All,

Sorry if i post this in the wrong thread, I want to ask in how to combine the simple injector container with ServiceStack controller. in servicestack they have their own container, e.g

 

ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));

How do I combine the container in the SimpleInjector which is being set in Global.asax and the container for ServiceStack is being set in AppHost.cs

any suggestion please?thank you in advance

Coordinator
May 16, 2012 at 7:56 AM
Edited Aug 24, 2012 at 2:48 PM

ServiceStack uses the ASP.NET MVC infrastructure, so you can write your own IControllerFactory and register it using the SetControllerFactory. This documentation page explains how to write your own MVC controller factory.

The ServiceStack documentation also has the notion of an IContainerAdapter. I'm not sure if you need to implement one for Simple Injector, but if you do, here is how to do it:

 

public class SimpleInjectorAdapter : IContainerAdapter
{
    private readonly Container container;

    public SimpleInjectorAdapter(Container container)
    {
        this.container = container;
    }

    public T Resolve<T>()
    {
        return (T)this.container.GetInstance(typeof(T));
    }

    public T TryResolve<T>()
    {
        IServiceProvider provider = this.container;
        object service =  provider.GetService(typeof(T));
        return service != null ? (T)service : default(T);
    }
}
Marked as answer by dot_NET_Junkie on 11/3/2013 at 11:51 PM