How to cancel an async WCF call?

后端 未结 3 566
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 20:48

I have been trying some time to find out how to implement WCF call cancellation based on the new .NET 4.5 CancellationToken mechanism. All the samples I found are not WCF ba

3条回答
  •  梦毁少年i
    2020-12-19 21:23

    As indicated before. It is impossible to cross service boundary and cancel on server side.

    If you want to cancel the Task on client side you can can use the extension method WithCancellation from Microsoft.VisualStudio.Threading.ThreadingTools

    It is part of Visual Studio SDK or you can also get it from Nuget.

     CancellationTokenSource ct = new CancellationTokenSource();
     ct.CancelAfter(20000);
     var asyncTask = _myService.LongOperationAsync();
     await asyncTask.WithCancellation(ct.Token);
    

提交回复
热议问题