首页 > 代码库 > WCF基本知识

WCF基本知识

1.开通WCF调试服务:

         须在服务端的行为中作如下配置:includeExceptionDetailInFaults="true" 代码如下:

               <behaviors>

                   <serviceBehaviors>

                         <behavior name="MyBehavior">

                              <serviceDebug  includeExceptionDetailInFaults="true" ></serviceDebug>

                          </behavior>

                   </serviceBehaviors>

                </behaviors>

 

2.关于WCF客户端信道异常的处理方法,须在catch中新建,且体代码如下:

            ChannelFactory<IContract> factory=new ChannelFactory<IContract>("myclient");

            IContract proxy=factory.CreateChannel();

            try

             {

                     proxy.GoFunction();

             }

             catch(Exception ex)

              {

                      if(proxy.InnerChannel.State == CommunicationState.Faulted)

                       {

                                  factory=new ChannelFactory<IContract>("myclient");

                                  // proxy=factory.CreateChannel();

                       }

 

              }

3.当WCf服务出现TimeOutException或CommunicationException时,当前信道壮态变成Faulted,表示信道出现错误,

   将使信道不能用于后续通信,即使调用close方法关闭也不行,此时客户端须调用Abort方法强制中断该信道。

     using(ChannelFactory<IContract>  factory=new ChannelFactory<IContract>("MyClient"))

     {

             IContract  proxy=factory.CreateChannle();

             try

             {

                     proxy.GoFunction();

                     (proxy as ICommunicationObject).Close();  // 这里是关闭信道

              }

              catch(CommunicationException ex)

              {

                      (proxy as ICommunicationObject).Abort();    // 强制杀死信道

               }

               catch(TimeoutException ex)

                {

                        (proxy as ICommunicationObject).Abort();    // 强行释放信道

                 }

                 catch(Exception ex)

                {

                         LogError.Message(ex.Message);

                }

     }