Castle Windsor Wcf Facility的配置及调用IChannel的Close

本文详细介绍了如何在服务端和客户端使用Castle Windsor容器管理WCF服务,通过配置windsor.xml实现服务注册与依赖注入,同时展示了针对transient生命周期管理的WCF通道关闭策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

服务端windsor.xml:
<configuration><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

  <facilities>

    <facility id="wcf"

                              type="Castle.Facilities.WcfIntegration.WcfFacility,

                                    Castle.Facilities.WcfIntegration" />

  </facilities>

  <components>

    <!--Services start-->

        <component

                 id="order service"

                 service="WCF.Contract.ServiceContract.IOrder, WCF.Contract"

                 type="WCF.Service.OrderService, WCF.Service" >

    </component>

    <!--Services end-->

  </components>

</configuration>

服务端Global.asax

protected void Application_Start(object sender, EventArgs e)

        {

            Container = new WindsorContainer()

        .AddFacility<WcfFacility>()
        .Install(Configuration.FromXmlFile("Windsor.xml"));

        }


服务端 Svr文件:

<% @ServiceHost Service="order service"

       Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory, Castle.Facilities.WcfIntegration" %>



客户端windsor.xml

<configuration>

  <facilities>

    <facility id="wcf"

                              type="Castle.Facilities.WcfIntegration.WcfFacility,

                                    Castle.Facilities.WcfIntegration" />

  </facilities>

       <components>

              <!--Services start-->

       <component

      id="order service"

       type="WCF.Contract.ServiceContract.IOrder, WCF.Contract"

       wcfEndpointConfiguration="order service"  lifestyle="transient">

    </component>

              <!--Services end-->

       </components>

</configuration>


这里为了每次都要启动一个实例,采用了transient的对象生命周期,
但是默认的WcfManagedChannelInterceptor不会自动调用IChannel.Close(),所以我改了下WcfManagedChannelInterceptor 的代码,添加了
Close方法:
#region IInterceptor Members
public void Intercept(IInvocation invocation)
  {
   EnsureOpenChannel(invocation);
   invocation.Proceed();
            Close(invocation);
  }

  #endregion

        void Close(IInvocation invocation)
        {
            if (lifestyle == LifestyleType.Transient)
            {
                IChannel channel = invocation.InvocationTarget as IChannel;
                if (channel != null)
                {
                    WcfUtils.ReleaseCommunicationObject(channel);
                }
            }
        }

这样的话每次调完一个wcf的实例,就把他关了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值