我在我的项目中使用城堡windsor来注册依赖于nhibernate会话的rhino esb使用者。
Rhino esb消费者有短暂的生活方式,nhibernate会议有着悠久的生活方式。我用拦截器注册Rhino esb使用者,我称之为container.BeginScope()。当消费者中没有ISession构造函数注入时,拦截器被创建并正确执行但是当消费者中有ISession构造函数注入时,拦截器不会被创建,也不会被执行,并且我得到InvalidOperationException:" Scope不可用。你忘记调用container.BeginScope()了吗?"这是我注册的rhino esb消费者
container.Register(
AllTypes
.FromThisAssembly()
.Where(type => typeof(IMessageConsumer).IsAssignableFrom(type) && !typeof(IOccasionalMessageConsumer).IsAssignableFrom(type))
.LifestyleTransient()
.Configure(registration =>
{
registration.Named(registration.Implementation.FullName);
registration.Interceptors<CommonTransactionInterceptor>();
}));
拦截体:
public CommonTransactionInterceptor(IWindsorContainer windsorContainer)
{
this.currentScope = windsorContainer.BeginScope();
this.session = windsorContainer.Resolve<NHibernate.ISession>();
this.windsorContainer = windsorContainer;
this.session.BeginTransaction();
}
public void Intercept(IInvocation invocation)
{
try
{
invocation.Proceed();
session.Transaction.Commit();
}
finally
{
this.currentScope.Dispose();
if (session != null)
{
if (session.Transaction != null)
{
session.Transaction.Dispose();
}
this.windsorContainer.Release(session);
this.Dispose();
}
}
}
消费者的身体:
public class CalculationProcessedCommandConsumer : ConsumerOf<CalculationProcessedCommand>
{
private ISession session;
public CalculationProcessedCommandConsumer(ISession session)
{
this.session = session;
}
public virtual void Consume(CalculationProcessedCommand message)
{
}
}
提前感谢您的帮助。