我使用包含很长循环的方法创建了一个测试服务。我希望当发生超时事务时,方法执行会刷新,但事实并非如此。客户端获得超时,但处理在服务器上继续。
有没有办法阻止它?不改变方法代码?
这是示例:在示例中,我使用队列绑定调用方法 QueueRequest,10 秒后事务中止。此时会发生重试,导致同样的问题。几次重试后,服务器正在执行 100% 的 cpu 工作,试图在多个线程/实例上运行循环,即使消息已中毒并被丢弃。
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall,
TransactionTimeout="00:00:10",
ReleaseServiceInstanceOnTransactionComplete=true)]
[ErrorHandlingBehaviorAttribute]
public class Service1 : IQueueService
{
public Service1()
{
Trace.WriteLine("Creating an instance on thread " + Thread.CurrentThread.ManagedThreadId.ToString());
}
~Service1()
{
Trace.WriteLine("Destroying an instance on thread " + Thread.CurrentThread.ManagedThreadId.ToString());
}
[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
public void QueueRequest(string message)
{
int id = Thread.CurrentThread.ManagedThreadId;
Trace.WriteLine("Got Message on thread " + id.ToString());
for (int i = 0; i < 1000000; i++)
{
Trace.WriteLine("Processing " + i.ToString() + " Thread ID " + id.ToString());
Thread.Sleep(1000);
}
}
}