Inversion of control 在ServiceStack中,获取容器的正确方法是什么
我目前正试图在我正在编写的一个信号器应用程序中使用ServiceStack,该应用程序是大型MVC4.5应用程序的一部分 我当前在App_Start文件夹中有一个类正在启动我的Hub。我的问题是如何获得对容器的引用,如ServiceStack的wiki中所示Inversion of control 在ServiceStack中,获取容器的正确方法是什么,inversion-of-control,servicestack,Inversion Of Control,servicestack,我目前正试图在我正在编写的一个信号器应用程序中使用ServiceStack,该应用程序是大型MVC4.5应用程序的一部分 我当前在App_Start文件夹中有一个类正在启动我的Hub。我的问题是如何获得对容器的引用,如ServiceStack的wiki中所示 container.Register<IRedisClientsManager>(c => new PooledRedisClientManager("localhost:6379")); container.Regist
container.Register<IRedisClientsManager>(c => new PooledRedisClientManager("localhost:6379"));
container.Register<ICacheClient>(c => (ICacheClient)c.Resolve<IRedisClientsManager>().GetCacheClient());
container.Register(c=>newpooleredisclientmanager(“localhost:6379”);
Register(c=>(ICacheClient)c.Resolve().GetCacheClient());
我正在做的是使用Redis作为开发缓存层,并计划使用我们现有的membase作为生产缓存层
如何获取容器?要从ServiceStack的IOC外部ServiceStack解析依赖关系,您可以使用以下任一方法:
var foo = HostContext.TryResolve<IFoo>(); //null if doesn't exist
var foo = HostContext.Resolve<IFoo>(); //throws if IFoo doesn't exist
通过AppHost singleton
这是通过IAppHost
singleton访问它的简写方式:
HostContext.AppHost.Container
EndpointHost现在是Endpoint。对的此外,AppHost似乎不再处于Endpoint下。AppHostBase.Instance会返回相同的吗?@FredLackey否,它是用于v3的,已更新为使用v4。不确定,但在版本3.9.48中,我没有在HostContext下获得此选项。。但是,I可以使用“AppHostBase.Instance.Container.resolve()”方法解析依赖项。
HostContext.AppHost.Container