简体   繁体   English

设置TaskCompletionSource取消状态故障WCF通道

[英]Setting a TaskCompletionSource to Cancel state faults WCF channel

I have an async WCF operation which I am cancelling on a certain condition (see below). 我有一个异步WCF操作,在一定条件下会取消该操作(请参见下文)。 However, I am getting Faulted event at client side after this. 但是,此后,我在客户端遇到了Faulted事件。 I also have a FaultHandler configured at Server side but the call does not go to HandleError or ProvideFault . 我还在服务器端配置了一个FaultHandler,但是调用不会转到HandleErrorProvideFault

Is this the default behavior of Task based WCF operation? 这是基于Task的WCF操作的默认行为吗? Shouldn't we get a OperationCanceledException at client side? 我们不应该在客户端得到OperationCanceledException吗?

Sample Code 样例代码

Operation: 操作方式:

Task<MyResult> CalculateResultAsync(SomeParameter parameter);

Service Side: 服务端:

Task<MyResult> CalculateResultAsync(SomeParameter parameter)
{
    TaskCompletionSource<MyResult> tcs = new TaskCompletionSource<MyResult>();

    Task.Run(() => {
        //do something
        //based on some condition cancel tcs
        tcs.SetCanceled();
    });

    return tcs.Task;
}

The implementation of the service does not change the SOAP protocol. 服务的实现不会更改SOAP协议。

If the service faulted (cancelled), than the client receives a fault. 如果服务出现故障(取消),则客户端会收到故障。

Besides that, using Task.Run on server code is usually a bad practice because it will be trading one thread pool thread for another and back again. 除此之外,在服务器代码上使用Task.Run通常是一种不好的做法,因为它将一个线程池线程换为另一个线程池线程,然后再次返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM