有一个WCF操作可能需要较长时间并多次调用,所以我在客户端另开线程调用,但客户端又可以取消调用,在取消时需要再次调用WCF,大体是这样做的
客户端取消时:  
thread.Abort(“取消”);线程中的代码:TestService.TestClient service = null;
try
{
     service = new WpfApplication1.TestService.TestClient ("NetTcpBinding_IService");
     while(结束条件)
     {
         service.Test(参数);
     }
}
catch(ThreadAbortException threadException)
{
    service.另一个方法();
}
catch(Exception ex)
{
}现在问题是,点击取消后出现异常:
The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.
请问这是为什么?另外,我做了个实验
同样的两个wcf操作,区别是Test2等待一会再返回
int Test1()
{
    return 1;
}
int Test2()
{
    System.Threading.Thread.Sleep(2000);
    return 1;
}
然后在客护端和我的提问一样也是在另一个线程
TestService.TestClient service = null;
try
{
     service = new WpfApplication1.TestService.TestClient ("NetTcpBinding_IService");
     while(true)  //不停调用
     {
         //分别试两个方法
         service.Test1();
         //service.Test2()
     }
}
catch(ThreadAbortException threadException)
{
    MessageBox.Show(service.State.ToString());
}
我发现两个结果是不同的,调用Test1时,线程异常中的service的状态是正常的opened。而另个结果则是Faulted,这是为什么呢?我的问题是不是也由于这个原因引起的?谢谢

解决方案 »

  1.   

    如果是在WCF服务方法调用过程中,出现了ThreadAbortException,那么代理的状态就会是Faulted你可以在Catch里重新初始化一个代理
    catch(ThreadAbortException threadException) 

        service = new WpfApplication1.TestService.TestClient ("NetTcpBinding_IService"); 
        service.另一个方法(); 

      

  2.   

    谢谢possible_Y
    那还有就是如果我ThreadAbortException里的方法是去释放一些长时间执行方法的资源(数据库连接,文件句柄)和事物回滚操作。而在调用的同时前一个方法还在服务端执行,会不会有什么问题?