Funq usage in ServiceStack
Asked Answered
I

1

7
  • 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.

Isolationist answered 31/5, 2012 at 16:19 Comment(1)
#3139709 Approach is ok for me.Isolationist
M
9

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>();
Myke answered 31/5, 2012 at 19:48 Comment(2)
There is no Resolve<T> method on IAppHost I used TryResolve<T> (comes from IResolver)Isolationist
Thanks updated answer. TryResolve returns null for missing dependencies where Resolve would throw a ResolutionExceptionMyke

© 2022 - 2024 — McMap. All rights reserved.