Filton Ong
Filton Ong

Reputation: 11

Error for GetStringAsync if triggered by ScheduledAgent but no error during WP8 App usage

I have a wrapper for the webclient that I am using to retrieve some data. This same function is being used by the WP8 App and also used by the WP8 ScheduledAgent.

Somehow, when the function is used by the WP8 App, there is no error and it returns correctly. However, when the ScheduledAgent uses the function, it erred out at the bold code below. I tried a try catch but it is not catching. Via Debugger, the GetSTringAsync(uri) had completed without any exception. The error seemed to be only happening when it is assigning the return Task to the result string.

The error I received is: An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll


   public class HttpClient : WebClient
   ..
        private async Task GetStringAsync(string strUri)
        {
            Uri uri = new Uri(strUri);
            string result = string.Empty;
            try
            {
                result = await GetStringAsync(uri);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return result;
        }
...
        private Task GetStringAsync(Uri requestUri)
        {
            TaskCompletionSource tcs = new TaskCompletionSource();

            try
            {
                this.DownloadStringCompleted += (s, e) =>
                {
                    if (e.Error == null)
                    {
                        tcs.TrySetResult(e.Result);
                    }
                    else
                    {
                        tcs.TrySetException(e.Error);
                    }
                };

                this.DownloadStringAsync(requestUri);

            }
            catch (Exception ex)
            {
                tcs.TrySetException(ex);
            }

            if (tcs.Task.Exception != null)
            {
                throw tcs.Task.Exception;             
            }

            return tcs.Task;
        }


Please advise if I am missing something.

Upvotes: 0

Views: 300

Answers (1)

Filton Ong
Filton Ong

Reputation: 11

My problem is because I am using pushpin as one of my object types within my Model. Apparently, in the scheduled agent, it is not able to access that object type and thus threw the above error.

Upvotes: 0

Related Questions