ServiceStack中Funq的用法 [英] Funq usage in ServiceStack

查看:146
本文介绍了ServiceStack中Funq的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 如何从控制器之外访问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 access Container instance?
  • Is it singleton?
  • Can I use new Container() or is there any chain like Funq.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆