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

服务端windsor.xml:
<configuration>

  <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的实例,就把他关了


posted @ 2009-01-11 20:49  Martin XJ  阅读(1236)  评论(2编辑  收藏  举报