【问题标题】:How to stop exution of a method call in WCF after a transaction timeout如何在事务超时后停止执行 WCF 中的方法调用
【发布时间】:2011-12-20 02:46:28
【问题描述】:

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

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

示例如下:在示例中,我使用队列绑定调用方法 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 multithreading message-queue


    【解决方案1】:

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

    看看herehere。为请求提供服务的线程与将结果传递给客户端的 WCF 部分完全分离。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-28
      • 2021-03-02
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多