ServiceStack中Funq的用法 [英] Funq usage in ServiceStack
问题描述
- 如何从控制器之外访问
Container
实例? - 我必须在课堂上使用
Container.Resolve
,但是如何访问Container
实例? - 是单身吗?
- 我可以使用
new Container()
还是有Funq.StaticContainer
这样的链条?
- How can I access
Container
instance out of controller? - I have to use
Container.Resolve
in my class but how can I accessContainer
instance? - Is it singleton?
- Can I use
new Container()
or is there any chain likeFunq.StaticContainer
?
感谢Mythz 获得要点提示,a)或b)或c).
Thanks to Mythz for gist hint, a) or b) or c).
我将使用Mythz的解决方案,该解决方案已被我接受,但存在有关模式(ServiceLocator
模式)的担忧,您可以
I will use Mythz's solution, it is accepted by me but there are concerns for it's pattern (ServiceLocator
Pattern), you can check here for extra info.
推荐答案
有两种方法可以静态引用您的AppHost实例.您可以通过 IAppHost 实例来解决依赖项,其中:
There are a couple of ways to statically reference your AppHost instance. You can resolve a dependency via the IAppHost instance with:
HostContext.TryResolve<T>();
HostContext.Resolve<T>();
HostContext.AppHost.TryResolve<T>();
如果由于某些原因需要访问具体的Funq.Container
,则可以通过单例访问它:
If for some reason you need to access the concrete Funq.Container
, you can access it via the singleton:
ServiceStackHost.Instance.Container
在服务,Razor视图页面等内部,您可以使用:
Whilst inside a Service, Razor View Page, etc you can use:
base.TryResolve<T>();
这篇关于ServiceStack中Funq的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!