Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ServiceStack what is the proper way to get the container

I'm currently attempting to use ServiceStack in a SignalR application that I am writing that is part of a large MVC 4.5 application.

I currently have a class in the App_Start folder that is starting my Hub. My question is how do I get a reference to the container as shown in the wiki for ServiceStack.

container.Register<IRedisClientsManager>(c => new PooledRedisClientManager("localhost:6379"));
container.Register<ICacheClient>(c => (ICacheClient)c.Resolve<IRedisClientsManager>().GetCacheClient());

What I am doing is using Redis as the Development cache tier and planning on using our exisitng membase as the production cache tier.

How do I get container?

like image 627
msarchet Avatar asked Nov 12 '12 19:11

msarchet


1 Answers

To resolve dependencies from ServiceStack's IOC outside servicestack you can use either:

var foo = HostContext.TryResolve<IFoo>(); //null if doesn't exist
var foo = HostContext.Resolve<IFoo>();    //throws if IFoo doesn't exist

There are a few to access the Container outside from of outside of ServiceStack, the quickest way is:

var container = HostContext.Container;

Via the AppHost singleton

Which is a shorthand way of accessing it via the IAppHost singleton:

HostContext.AppHost.Container
like image 85
mythz Avatar answered Sep 30 '22 19:09

mythz