我需要为我的服务解析依赖项,以根据传入请求中的HTTP头的值。访问来自容器的IHttpRequest或IRequestContext
我已经尝试注册一个工厂方法,像这样:
container.Register(c => GetDependencyForRequest(c.Resolve<IHttpRequest>()));
而且我已经试过:
container.Register(c => GetDependencyForRequest(c.Resolve<IRequestContext>()));
然而,无论是扔ResolutionException
秒。
我宁愿不必在决定使用哪种实现方式的情况下为我的服务提供服务。我只是希望他们在构造函数中有一个IDependency
,并让容器解决它。
有没有办法做到这一点?还是有另一种方式去解决这个问题?
如果您正在使用[ServiceStack的内置Funq IOC](https://github.com/ServiceStack/ServiceStack/wiki/The-IoC-container),您不希望通过指定** ReuseScope .None **即'container.Register(c => new FindDependencyForRequest(HttpContext.Current.ToRequestContext()))。' – mythz 2013-02-15 02:20:52
'我以为使用'Func'重载' Container.Register'导致提供的工厂被称为每个解决方案,有效地使重用范围瞬变。 “ReusedWithin(ReuseScope.None)”实际上是否有必要? –
rossipedia
2013-02-15 07:32:57
不知道为什么你会认为,所有文档默认情况下都是单例,并且可以使用'.ReusedWithin()'来更改生命周期。 [以下是一些显示Funq默认行为的测试。](https://github.com/ServiceStack/ServiceStack/commit/b7ec3fae3db9fa6ccccd19dcc39ee3167017c8d2) – mythz 2013-02-15 08:04:33