【问题标题】:Scope gets lost in Castle Windsor typed factory?Scope 在温莎城堡类型的工厂里迷路了?
【发布时间】: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 用于IMyItemFactoryIMyItems,它会正确解析IMyItems,但我需要使用一些WcfOperation 范围内的组件IMyItems,所以很遗憾这不是一个解决方案。
  • 当你打电话给_factory.GetMyItem()时是OperationContext.Currentnull
  • 它是空的!谢谢!就是这样,我认为是这个错误:stackoverflow.com/questions/12797091/…

标签: dependency-injection castle-windsor typed-factory-facility


【解决方案1】:

正如 cmets 中提到的那样,OperationContext.Currentnull,所以从技术上讲,温莎正在做正确的事情 - 提醒您注意这一事实。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 2011-08-22
    相关资源
    最近更新 更多