Eli*_*xir 8 c# wcf reportingservices-2005 task-parallel-library
我正在后台线程上连接到SSRS 2005服务并调用Render方法
https://msdn.microsoft.com/en-us/library/reportexecution2005.reportexecutionservice.render.aspx
Render方法的代码很好,内置了取消令牌支持并按预期取消.
然而,Render方法WCF调用本身不支持取消令牌,这种操作在我的情况下可能需要1-2个小时,如果有人决定取消,我不想长时间保持我的服务.
有没有办法取消WCF调用'在飞行中',以便它可以抛出一个operationcancelledexception(或类似的东西),以便不保持我的客户端应用程序资源?
首先,您需要为 WCF 客户端打开异步方法生成。您需要创建await
一个新任务,该任务将在 SSRS 操作完成或请求取消时结束。您可以使用如何取消不可取消的异步操作?WithCancellation
中的扩展方法来实现此目的。文章:
public static async Task<T> WithCancellation<T>(
this Task<T> task, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<bool>();
using(cancellationToken.Register(
s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))
if (task != await Task.WhenAny(task, tcs.Task))
throw new OperationCanceledException(cancellationToken);
return await task;
}
Run Code Online (Sandbox Code Playgroud)
像这样使用它:
// WithCancellation will throw OperationCanceledException if cancellation requested
RenderResponse taskRender = await ssrsClient.RenderAsync(renderRequest)
.WithCancellation(cancellationToken);
Run Code Online (Sandbox Code Playgroud)
renderRequest
是生成的类的实例RenderRequest
。
我不确定如何访问out
同步版本Render
操作中出现的参数值,因为我目前无法访问 SSRS。