sharp architecture contrib transaction attribute in windows



出于某种原因:

[Transaction]
public void DoSomething()
{
    ...
}

不起作用,我仍然必须像这样显式使用事务:

public void DoSomething()
{
    using (var tx = NHibernateSession.Current.BeginTransaction())
    {
        ....
        tx.Commit();
    }
}

知道为什么吗?

我正在使用这样的东西来引导东西:

_container = new WindsorContainer();
ComponentRegistrar.AddComponentsTo(_container);
...
ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(_container));
ComponentRegistrar.AddComponentsTo(_container, typeof(NHibernateTransactionManager));
NHibernateSession.Init(new ThreadSessionStorage(),
        new[] { "Bla.Domain.dll" },
        new AutoPersistenceModelGenerator().Generate(),
        "NHibernate.config");

正如Doan所说,具有该方法的组件没有代理。

由于该方法不是虚拟的,我假设您的类正在实现一个接口。 确保在调用 DoSomething 的类中具有定义为接口而不是实现类的依赖项。

如果调试代码并检查对象的运行时类型,则它应该是 Castle 代理

有关更多详细信息,请查看夏普架构 contrib wiki 上的故障排除部分https://github.com/sharparchitecture/Sharp-Architecture-Contrib/wiki/Troubleshooting

通常,此类问题是由调用提供事务管理服务的动态代理失败引起的。两个最常见的错误是:

  • 该方法不能被代理:很可能没有实现任何接口方法,或者对象没有被代理。
  • 该方法是从同一个类调用的,它绕过了所有代理。

编辑:

我猜你使用温莎城堡作为IoC容器。[Transaction]装饰需要自动事务管理工具才能工作。如果您成功配置了设施,即您[Transaction]一种方法中工作,而不是另一种方法,那么上述答案适用。如果所有事务修饰都不起作用,则必须先查看设施的配置。

最新更新