Cancellation of Long running async WCF Call to SSRS
Asked Answered
W

1

10

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?

Wallenstein answered 7/2, 2016 at 12:7 Comment(0)
L
4

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.

Logogram answered 13/2, 2016 at 15:54 Comment(2)
This doesn't actually cancel the operation, it just stops waiting for the operation to finish.Quickly
No, It doesn't. OP asked for a way to release his client application resources in case someone cancel request.Logogram

© 2022 - 2024 — McMap. All rights reserved.