【发布时间】:2015-08-31 14:09:52
【问题描述】:
我在带有 TypedFactoryFacility 的容器中有以下 Windsor 组件注册码:
Component
.For<IMyItemFactory>()
.AsFactory(f => f.SelectedWith(new MyComponentSelector()))
.LifestylePerWcfOperation(),
Classes
.FromAssembly(Assembly.GetExecutingAssembly())
.BasedOn<IMyItem>()
.LifestylePerWcfOperation()
.Configure(c => c.Named(c.Implementation.Name)),
致力于创建 IMyItemFactory 的自动实现。在执行 IMyItemFactory 工厂方法程序期间异常失败
Castle.MicroKernel.ComponentResolutionException: Could not obtain scope for component SpecificItem. This is most likely either a bug in custom IScopeAccessor or you're trying to access scoped component outside of the scope (like a per-web-request component outside of web request etc)
at Castle.MicroKernel.Lifestyle.ScopedLifestyleManager.GetScope(CreationContext context)
at Castle.MicroKernel.Lifestyle.ScopedLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy)
at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden)
at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context)
at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy)
at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(String key, Type service, IDictionary arguments, IReleasePolicy policy)
at Castle.Facilities.TypedFactory.TypedFactoryComponentResolver.Resolve(IKernelInternal kernel, IReleasePolicy scope)
at Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Resolve(IInvocation invocation)
at Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.IMyItemFactoryProxy.GetMyItem(String myItemType)
这很令人困惑,因为应用程序中的每个组件实际上都有 WcfOperation 范围,所以我不明白这是怎么发生的。我什至尝试用其范围记录每个已注册的类型,以断言 IMyItem 类具有 WcfOperation 范围——而且它们确实具有。
你知道如何调试吗?
编辑:我使用 WcfOperation-scope 从成功构造的对象调用工厂,该对象调用另一个 WcfOperation-scoped 服务没有问题:
public SomeDataProvider(IElasticsearchClient elasticClient, IMyItemFactory factory)
{
_elasticClient = elasticClient;
_factory = factory;
}
async Task SomeMethod()
{
var someString = await _elasticClient.SomeMethod(); // ok
var myItem = _factory.GetMyItem(someString); // exception from above
// ...
}
【问题讨论】:
-
您在哪里以及如何使用工厂?
-
我添加了使用说明。我想这个问题很难从远处回答——我会对一个能教我如何调试这种情况的答案感到满意。正如我已经写过的那样,我唯一的想法是使用
Container.Kernel.GetAssignableHandlers调用来断言自己一切都在同一个范围内。 -
如果将
LifestylePerWcfOperation替换为LifestyleTransient用于IMyItemFactory和IMyItems,它会正确解析IMyItems,但我需要使用一些WcfOperation范围内的组件IMyItems,所以很遗憾这不是一个解决方案。 -
当你打电话给
_factory.GetMyItem()时是OperationContext.Currentnull? -
它是空的!谢谢!就是这样,我认为是这个错误:stackoverflow.com/questions/12797091/…
标签: dependency-injection castle-windsor typed-factory-facility