How to cancel an async WCF call?

后端 未结 3 562
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  执笔经年
    2020-12-19 21:13

    In two words - this is impossible.

    If you understand what WCF call is by its nature, it becomes obvious.

    Looking on your example, what we see generally: Two separate machine A and B, connected via networks. On machine A you have some component which listen a socket, and when it gets a command it starts synchronous long computation RemoteCall().

    On machine B you have a client which has two autogenerated methods in fact

    BeginRemoteCall->IAsyncResult + EndRemoteCall(IAsyncResult).

    New Task based version of asynchronous call (aka RemoteCallAsync()->Task) by the nature is absolutely the same, and you can always create your own task using TaskFactory.FromAsync(BeginRemoteCall,EndRemoteCall)->Task

    When you call BeginRemoteCall on the client, you send the command to the second machine "please start computation", and then you add a callback, which will be called, when calculation is done and results came. Internally this async implementation uses I/O completion ports and allows your client to be non-blocking.

    But there is no way to cancel an operation on the server side, if it is already started. You may not be interested in results, you may ignore your callback, but the computation on the server side will be finished anyway.

    That's it

提交回复
热议问题