【问题标题】:Cancel WCF operation on client abort在客户端中止时取消 WCF 操作
【发布时间】:2011-06-17 18:53:59
【问题描述】:

我有一个 Silverlight 客户端,它可以调用我的 WCF Web 服务上的一个方法来从数据库中获取值。在长查询的情况下,我想给用户取消它的选项。

客户端,当用户按下“取消”时,我在 WCF 实例上调用“.Abort()”。但是,我的 WCF 服务仍将继续运行,因为它不知道客户端不再连接。

我的问题是:WCF 服务是否有可能知道客户端已终止连接?

我曾尝试在我的合约方法中订阅 Channel.Closed/faulted 事件,但它们从未触发...

[AspNetCompatibilityRequirements(RequirementsMode =  AspNetCompatibilityRequirementsMode.Allowed)]
internal class MyContract: IMyContract
{
    public object QueryDatabase(...)
    {
        System.ServiceModel.OperationContext.Current.Channel.Faulted += new System.EventHandler(Channel_Faulted);
        System.ServiceModel.OperationContext.Current.Channel.Closed += new System.EventHandler(Channel_Faulted);

        // Perform sql query
    }

    void Channel_Faulted(object sender, System.EventArgs e)
    {
        // Cancel SQL query here
    }
}

有人知道怎么做吗? 谢谢!

【问题讨论】:

  • 我目前正在使用 BasicHttpBinding。我看了你发布的文章,它不支持可靠的会话。如果我可以使用另一种类型的绑定(如 WSHttpBinding),我将不得不进一步调查,因为 BasicHttpBinding 最初用于更好的互操作性......

标签: c# .net wcf


【解决方案1】:

Faulted 和 Closed 事件确实可以按照您在代码示例中使用它们的方式使用,但只有当您使用的绑定支持可靠会话,并且您启用了可靠会话时。检查您使用的绑定是否支持它,如果支持则启用它。

<bindings>
  <netTcpBinding>
    <binding>
      <reliableSession ordered="true"
            inactivityTimeout="0:10:0"
            enabled="true" />
    </binding>
  </netTcpBinding>
</bindings>

如果您的绑定不支持这一点,那么您可以考虑切换到支持的绑定。 You can find a list of what is supported for the different bindings here.

【讨论】:

    【解决方案2】:

    客户端需要向服务发送另一个请求,让它知道它需要取消 - 这是假设

    1. 服务可以识别正在为此取消客户端执行的工作
    2. 正在执行的工作实际上是可取消的(许多异步操作没有;没有取消 API

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-14
      • 1970-01-01
      • 2011-04-04
      • 2010-11-16
      相关资源
      最近更新 更多