在具有自定义服务行为的Wcf服务中使用Windsor NHibernateFacility来创建事务,而不是事务属性

在具有自定义服务行为的Wcf服务中使用Windsor NHibernateFacility来创建事务,而不是事务属性,nhibernate,transactions,wcffacility,windsor-3.0,windsor-nhfacility,Nhibernate,Transactions,Wcffacility,Windsor 3.0,Windsor Nhfacility,我正试图在Wcf服务中首次使用温莎NHibernate设施,并取代目前NHibernate的手动注册,以便在所有服务中都有一致的方法 目前的工作方法 之前我一直在手动注册NHibernate组件 container.Register( Component.For().UsingFactoryMethod(()=>CreateMappings(“SomeConnectionString”).BuildSessionFactory()); 集装箱。登记( Component.For().LifeSt

我正试图在Wcf服务中首次使用温莎NHibernate设施,并取代目前NHibernate的手动注册,以便在所有服务中都有一致的方法

目前的工作方法 之前我一直在手动注册NHibernate组件

container.Register(
Component.For().UsingFactoryMethod(()=>CreateMappings(“SomeConnectionString”).BuildSessionFactory());
集装箱。登记(
Component.For().LifeStyle.PerwcfoOperation().UsingFactoryMethod(OpenSession));
然后,我使用自定义服务行为为每个操作创建并完成一个事务范围

公共类事务行为:IServiceBehavior
{
公共无效ApplyDispatchBehavior(ServiceDescription ServiceDescription,ServiceHostBase ServiceHostBase)
{
foreach(serviceHostBase.ChannelDispatchers中的var cdb)
{
var channelDispatcher=cdb作为channelDispatcher;
如果(null==channelDispatcher)继续;
foreach(channelDispatcher.Endpoints中的var endpointDispatcher)
{
foreach(endpointDispatcher.DispatchRuntime.Operations中的var dispatchOperation)
{
dispatchOperation.CallContextInitializers.Add(new TransactionContext());
}
}
}
}
public void AddBindingParameters(ServiceDescription ServiceDescription,ServiceHostBase ServiceHostBase,集合终结点,BindingParameterCollection bindingParameters){}
公共无效验证(ServiceDescription ServiceDescription,ServiceHostBase ServiceHostBase){}
}
公共类TransactionContext:ICallContextInitializer
{
私人交易范围交易;
调用前的公共对象(InstanceContext InstanceContext、IClientChannel通道、消息消息)
{
transaction=新TransactionScope();
返回null;
}
调用后公共无效(对象关联状态)
{
if(事务!=null)
{
transaction.Complete();
transaction.Dispose();
}
}
}
NHibernate设施的整合 我已经下载了,并使用下面的资源,我尝试插入该设施,并删除任何手动连接到NHibernate的电线。然而,我不想用
Transactional
Transaction
属性装饰服务和方法。以下是我当前的连线

container.Register(Component.For().ImplementedBy();
container.AddFacility


首先,您应该知道Henrik Feldt的Castle Transactions和AutoTx设施解释需要使用[Transaction]属性(正如我不久前在Castle Users邮件列表中与Feldt先生讨论的那样)

如果您想从您的服务类中去掉[Transaction]属性(到achive POCO),但在每次呼叫会话场景中继续使用NH和WCF,您应该:

  • 保留IServiceBehaviour实现,但在构造函数中插入IDispatchMessageInspector接口,并将其分配给类全局IDispatchMessageInspector属性

  • 在serviceHostBase中每个ChannelDispatcher的ApplyDispatchBehavior方法中,循环端点并添加注入的DispatchMessageInspector实例:

    foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
    {
        foreach (var endpoint in channelDispatcher.Endpoints)
        {
            endpoint.DispatchRuntime.MessageInspectors.Add(this.dispatchMessageInspector);
        }
    }
    
  • 创建一个实现IDispatchMessageInspector的类(将在上一项中注入的类)。让我们调用这个类NHWCFinteggementMessageInspector

  • 将ISessionFactory注入NHWCFintegorationMessageInspector,并将其设置为本地属性

  • 在AfterReceiveRequest方法中,使用ISessionFactory.Open()创建NH会话对象,开始事务并将该ISession绑定到CurrentSessionContext

  • 在BeforeSendReply方法中,从CurrentSessionContext取消绑定注入的SessionFactory,这将为您提供实际的会话对象。从会话的事务属性检查事务是否处于活动状态,如果处于活动状态,请提交它。然后处置()会话

  • 不要使用INHibernateInstaller。而是使用IWindsorInstaller并使用MyHibernateInstaller实现它。在注册块,将IServiceBehavior实现和新的IDispatchMessageInspector实现注册到容器

  • 同时在同一安装程序中注册ISessionFactory,并将FNH配置方法作为factory方法提供,但提供构建的ISessionFactory。但在从fluentconfiguration对象调用构建ISessionFactory之前的FNH配置中:

    fluentConfiguration.CurrentSessionContext();

  • 塔达

  • 完成这些步骤后,您不必再持有对Haf库的引用。它是纯Castle.Core,Castle.Windsor,NH/FNH

    有问题吗?;)


    (很抱歉没有发布完整的代码,只是描述,我有合同义务)

    首先,您应该知道Henrik Feldt的Castle交易和AutoTx设施解释需要使用[Transaction]属性(正如我不久前在Castle用户邮件列表中与Feldt先生讨论的那样)

    如果您想从您的服务类中去掉[Transaction]属性(到achive POCO),但在每次呼叫会话场景中继续使用NH和WCF,您应该:

  • 保留IServiceBehaviour实现,但在构造函数中插入IDispatchMessageInspector接口,并将其分配给类全局IDispatchMessageInspector属性

  • 在serviceHostBase中每个ChannelDispatcher的ApplyDispatchBehavior方法中,循环端点并添加注入的DispatchMessageInspector实例:

    foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
    {
        foreach (var endpoint in channelDispatcher.Endpoints)
        {
            endpoint.DispatchRuntime.MessageInspectors.Add(this.dispatchMessageInspector);
        }
    }
    
  • 创建一个实现IDispatchMessageInspector的类(将在上一项中注入的类)。让我们把这个类称为NHWCFineTegrationMess