I'm connecting to an SSRS 2005 Service on a background thread and calling the Render method
https://msdn.microsoft.com/en-us/library/reportexecution2005.reportexecutionservice.render.aspx
The code around the Render method is fine with cancellation token support built in and cancelled as expected.
However the Render method WCF call itself doesn't support cancellation tokens and this operation can take up to an 1 - 2 hours in my case, and I do not want to hold my service up for so long if someone decides to cancel.
Is there a way to cancel a WCF call 'In flight' so that it can throw an operationcancelledexception (or something similar) as to not hold my client application resources up?
First you need to turn on asynchronous methods generation for WCF client. Than you need to create and await
a new task that will end when either SSRS operation is finished or cancelation is requested. You can achieve this using WithCancellation
extension method from How do I cancel non-cancelable async operations? article:
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;
}
Use it like this:
// WithCancellation will throw OperationCanceledException if cancellation requested
RenderResponse taskRender = await ssrsClient.RenderAsync(renderRequest)
.WithCancellation(cancellationToken);
renderRequest
is instance of generated class RenderRequest
.
I'm not sure how to access values from out
parameters that appear in synchronous version of Render
operation, because I don't have access to SSRS at the moment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With