Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancellation of Long running async WCF Call to SSRS

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?

like image 707
Elixir Avatar asked Feb 07 '16 12:02

Elixir


1 Answers

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.

like image 160
Leonid Vasilev Avatar answered Oct 29 '22 09:10

Leonid Vasilev