2

我正在使用 Windor 教程中的 Nhibernate 持久性工具:

Kernel.Register(
    Component.For<ISessionFactory>()
        .UsingFactoryMethod(config.BuildSessionFactory)
        .LifeStyle.Singleton,
    Component.For<ISession>()
        .UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession())
        .LifeStyle.PerWebRequest);

有时我对 config.BuildSessionFactory 的调用会失败(可能是我的映射错误,或者我的连接字符串无效等等)。在调试器中,我可以看到 Nhibernate 异常被抛出。现在 Windsor 无法再解决我ISession的问题,因为工厂本身无法实例化。

问题是它似乎没有抱怨它。如果没有调试器,异常会被默默地丢弃,我的应用程序中唯一的症状是我所有的ISession依赖项都是突然的null. 处理异常的正确方法是UsingFactoryMethod什么?有什么方法可以告诉温莎在我的应用程序中冒泡这个例外吗?

4

1 回答 1

1

我可以看到 Castle 吃异常的唯一原因是会话是否作为属性注入,这使得 Castle 认为它是可选的。

这是我修复它的方法......我创建了一个激活器,当它无法设置属性的值时会引发异常:

public class StrictComponentActivator : DefaultComponentActivator
{
    public StrictComponentActivator(ComponentModel model, IKernelInternal kernel,
        ComponentInstanceDelegate onCreation,
        ComponentInstanceDelegate onDestruction)
        : base(model, kernel, onCreation, onDestruction) { }

    protected override void SetUpProperties(object instance, CreationContext context)
    {
        instance = ProxyUtil.GetUnproxiedInstance(instance);
        var resolver = Kernel.Resolver;
        foreach(var property in Model.Properties)
        {
            var value = ObtainPropertyValue(context, property, resolver);

            if(value != null)
            {
                var setMethod = property.Property.GetSetMethod();
                try
                {
                    setMethod.Invoke(instance, new[] { value });
                }
                catch(Exception ex)
                {
                    throw new ComponentActivatorException(
                        string.Format(
                            "Error setting property {1}.{0} " +
                            "in component {2}. " +
                            "See inner exception for more information. " +
                            "If you don't want Windsor to set this property " +
                            "you can do it by either decorating it with " +
                            "DoNotWireAttribute or via registration API.",
                            property.Property.Name,
                            instance.GetType().Name,
                            Model.Name),
                        ex, Model);
                }
            }
        }
    }

    private object ObtainPropertyValue(CreationContext context, PropertySet property, IDependencyResolver resolver)
    {
        if(property.Dependency.IsOptional == false ||
            resolver.CanResolve(context, context.Handler, Model, property.Dependency))
        {
            try
            {
                return resolver.Resolve(context, context.Handler, Model, property.Dependency);
            }
            catch(Exception e)
            {
                if(property.Dependency.IsOptional == false)
                {
                    throw;
                }
                Kernel.Logger.Warn(
                    string.Format("Exception when resolving optional dependency {0} on component {1}.",
                        property.Dependency, Model.Name), e);
            }
        }
        return null;
    }
}

然后我配置了我的大部分组件.Activator<StrictComponentActivator>()

于 2012-08-17T14:18:04.850 回答