Question

For windows phone 8.1 microsoft introduced some methods, which end with AndContinue. These methods suspend the app to execute and process user input. Afterwards they call a Continue...-method with a object containing the result of the operation.

A example would be WebAuthenticationBroker.AuthenticateAndContinue, which is used for OAuth.

Example code:

class Test : IWebAuthenticationContinuable
{
    private void StartAuth()
    {
        WebAuthenticationBroker.AuthenticateAndContinue(new Uri("http://example.org/token?someInformations"),
                new Uri("http://example.org/callback"), null, WebAuthenticationOptions.None);
    }

    private void ContinueWebAuthentication(WebAuthenticationBrokerContinuationEventArgs args)
    {
        WebAuthenticationResult result = args.WebAuthenticationResult;

        //Do something with the result
    }
}

In windows store apps the same is achived by using the WebAuthenticationBroker.AuthenticateAsync-emthod. This method is a simple async operation.

I'd like to write a AuthenticateAsync-method for windows phone using AuthenticateAndContinue. It must return a Task<WebAuthenticationResult>.

As a quite hacky approach, I thought about a Task, which is completed after the execution of ContinueWebAuthentication. If I await this task and set the result in some variable, I could access it in the async method and return it.

But I wasn't able to figure out how to implement this.

Was it helpful?

Solution

The AndContinue APIs are not asynchronous calls like the Windows Store AuthenticateAsync is. From MSDN:

When you call an AndContinue method, your app is deactivated while the operation completes in order to conserve memory. On low-memory devices, your app might even be terminated.

So, when you call an AndContinue method, your app can be terminated. When your app is restored, you'll need some way of jumping back into your async method. There's nothing like this built-in AFAIK.

It is certainly possible to create a Task<WebAuthenticationResult> using TaskCompletionSource<T>, but it won't work in this case. The AndContinue method can terminate your app, and when your app resumes, it can complete the task but there won't be any async methods waiting on it.

You could possibly have a "task cache" that is serialized on suspend/resume, and have your async methods pull from that cache, invoking other async methods only if the task isn't found. I'd say that approach is fraught with pitfalls, though, and totally not worth it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top