2010-12-09 74 views
2

我有一个WCF服务,有一些操作可能需要很长时间... 客户端接收到TimeoutException,但服务器在长时间操作后继续执行。检测服务器端的TimeoutException WCF

服务器:

public void doSomeWork(TransmissionObject o) { 
    doDBOperation1(o); // 
    doDBOperation2(o); // may result in TimeoutException on client 
    doDBOperation3(o); // it continues doing DB operations. The client is unaware! 
} 

客户:

ServiceReference.IServiceClient cli = new ServiceReference.IServiceClient("WSHttpBinding_IService","http://localhost:3237/Test/service.svc"); 
int size = 1000; 
Boolean done = false; 
TransmissionObject o = null; 

while(!done) { 
    o = createTransmissionObject(size); 
    try { 
     cli.doSomeWork(o); 
     done = true; 
    }catch(TimeoutException ex) { 
     // We want to reduce the size of the object, and try again 
     size--; 
     // the DB operations in server succeed, but the client doesn't know 
     // this makes errors. 
    }catch(Exception ex) { ... } 
} 

由于服务器正在执行某些数据库操作,我需要检测在服务器端超时要能够回滚数据库操作。

我试图用与[TransactionFlow]的TransactionScope等交易,在客户端,但在服务器上的数据库操作使用它们嵌套!!存储过程,所以我不能使用分布式事务。 (我收到一个SqlException说:不能在分布式事务中使用SAVE TRANSACTION。)。如果我使用简单的SP(不是嵌套的),那么交易解决方案就可以正常工作。

我的问题: 如何检测TimeoutException,但在服务器端?我猜是与代理状态有关的东西......或者可能是服务器可以捕获的一些事件。 我不确定在服务器端处理交易是否是正确的解决方案。 是否有解决此问题的模式?

谢谢!

回答