我用一个包含很长循环的方法创建了一个测试服务。我希望当发生超时事务时,方法执行将刷新,但事实并非如此。客户端获得超时,但处理在服务器上继续。

有办法阻止它吗?不改变方法代码?

以下是示例:在示例中,我使用队列绑定调用方法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);        
         }
      }
   }
有帮助吗?

解决方案

如果不修改现有代码,我认为这是不可能的。

查看此处这里。为请求提供服务的线程与WCF完全分离,后者将结果传递给客户端。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top