【问题标题】:Castle Windsor Facilities not playing nicely with Nancy Windsor Bootstrapper温莎城堡设施与 Nancy Windsor Bootstrapper 配合得不好
【发布时间】:2012-09-22 21:25:05
【问题描述】:

我在尝试从 Nancy 0.7 升级到 0.12 时遇到了一个奇怪的问题。以前我正在注册一个工具来为我的引导程序中的所有服务进行日志记录:

    protected override void ConfigureApplicationContainer(IWindsorContainer existingContainer)
    {
        existingContainer.AddFacility<LoggingFacility>();
        existingContainer.Register(Component.For<LoggingInterceptor>());
        ...other registration
    }

LoggingFacility 如下所示:

public class LoggingFacility : AbstractFacility 
{
    protected override void Init() { Kernel.ComponentRegistered += KernelComponentRegistered; }

    static void KernelComponentRegistered(string key, IHandler handler)
    {
        if (!ShouldProxyComponent(handler))
            return;

        // Don't add more than one logging interceptor to a component
        handler.ComponentModel.Interceptors.AddIfNotInCollection(InterceptorReference.ForType<LoggingInterceptor>());
    }

    static bool ShouldProxyComponent(IHandler handler)
    {
        //Don't log interceptors themselves
        if (typeof(IInterceptor).IsAssignableFrom(handler.ComponentModel.Implementation)) 
            return false;

        //Don't put proxy around any late-bound (usually factory-created) component
        if (handler.ComponentModel.Implementation == typeof(LateBoundComponent))
            return false;

        return true;
    }
}

不幸的是,自从升级到 0.12/Castle 3.1 后,WindsorNancyBootstrapper.RegisterTypes 中的以下行导致了一些问题

        container.Register(Component.For<Func<IRouteCache>>()
            .UsingFactoryMethod(ctx => (Func<IRouteCache>) (ctx.Resolve<IRouteCache>)));

基本上,Castle 尝试围绕 Func 创建一个动态代理。如果此注册触发了我的设施订阅的事件,那会很好,但它不会。然而拦截器似乎已经注册了。

当尝试创建代理时,它显然会失败,因为 MulticastDelgate(IL 的 Func 的父级)是密封的: 类型加载异常 无法从程序集“DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a621a9e7e5c32e69”加载类型“Castle.Proxies.Func`1Proxy”,因为父类型是密封的。

我不确定在这里做什么,有人对设施和 Nancy 0.12 有任何经验吗?

【问题讨论】:

    标签: castle-windsor interceptor nancy windsor-facilities multicastdelegate


    【解决方案1】:

    对我来说,解决方案是注册 Castle TypedFactoryFacility 作为我覆盖 Nancy 引导程序的一部分:

    existingContainer.AddFacility<TypedFactoryFacility>();
    

    在 Nancy.Bootstrappers.Windsor 中有一个 pull request,其中包含该更改作为其余更改的一部分

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多