Shane van Wyk
Shane van Wyk

Reputation: 1900

Using ServiceStack and SimpleInjector together to resister API service

We are trying to use ServiceStack alongside our ASP.NET MVC 5 application.

So the end user will be using the web application which makes good use of ASP.NET MVC.

We want to release a set of API's so that our old system can communicate with the new system.

The new system makes use of SimpleInjector IoC to glue the whole application together.

I have installed ServiceStack.Mvc but can't get it to work.

I tried following this link: https://github.com/ServiceStack/ServiceStack/wiki/Mvc-integration

I think it is because ServiceStack has it's own built in IoC which is conflicting with SimpleInjector.

Here is my Application_Start

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);            

    Bootstrap.Configure();

    #region Initialize SimpleInjector
    var container = new Container();

    InitializeContainer(container);
    container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
    container.RegisterMvcIntegratedFilterProvider();
    container.Verify();

    DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
    #endregion            

    new AppHost().Init();
}

Here is my AppHost

public class AppHost : AppHostBase
{
    public AppHost() : base("MVC 5", typeof(MyServices).Assembly) { }

    public override void Configure(Container container)
    {
        SetConfig(new HostConfig
        {
            HandlerFactoryPath = "api",
        });

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

[ServiceStack.Route("/hello/{Name}")]
public class Hello
{
    public string Name { get; set; }
}

public class MyServices : Service
{
    public object Any(Hello request)
    {
        return request;
    }
}

What am I doing wrong? When I go to website.com/api I just get a 404 resource not found page.

I would like to keep using SimpleInjector if possible, is that possible? Or do I need to adopt ServiceStack IoC throughout the whole application?

The application is following the Onion Architecture which separates a lot of concerns, which is why we are using SimpleInjector, we are using its Packaging feature quite a bit.

Any help will be much appreciated.

Update: After a bit more research I see one can write an Adapter, as this link explains: https://github.com/ServiceStack/ServiceStack/wiki/The-IoC-container but there is no example of SimpleInjector implementation, Can anyone help?

Upvotes: 2

Views: 247

Answers (1)

Steven
Steven

Reputation: 172646

An adapter for Simple Injector would look as follows:

public class SimpleInjectorServiceStackAdapter : IContainerAdapter
{
    private readonly Container container;

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

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

    public T TryResolve<T>() {
        var registration = this.container.GetRegistration(typeof(T));
        return registration == null ? default(T) : (T)registration.GetInstance();
    }
}

Upvotes: 5

Related Questions