Funq usage in ServiceStack

隐身守侯 提交于 2020-01-12 05:35:20

问题


  • How can I access Container instance out of controller?
  • I have to use Container.Resolve in my class but how can I access Container instance?
  • Is it singleton?
  • Can I use new Container() or is there any chain like Funq.StaticContainer?

Thanks to Mythz for gist hint, a) or b) or c).

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.


回答1:


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>();

If for some reason you need to access the concrete Funq.Container, you can access it via the singleton:

ServiceStackHost.Instance.Container

Whilst inside a Service, Razor View Page, etc you can use:

base.TryResolve<T>();


来源:https://stackoverflow.com/questions/10837445/funq-usage-in-servicestack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!