L
L
llexus2015-02-27 13:19:36
.NET
llexus, 2015-02-27 13:19:36

Why does the application crash on the second call to DownloadStringTaskAsync?

Hello. The application is thrown into an UnhandledException on the second call to this function.

public static class Locator
    {
        public static async Task<List<Location>> GetCitiesAsync(string token = "", int max = 5)
        {
            WebClient wc = new WebClient();
            string resultContent = await wc.DownloadStringTaskAsync(new Uri(string.Format("http://www.site.ru/ajax/geo.cshtml?_=r&context=&token={0}&max_matches={1}&use_similar=0", HttpUtility.UrlEncode(token), max.ToString())));
            // parse ...

            Locations ll = JsonConvert.DeserializeObject<Locations>(resultContent);

            return (from l in ll.locations select l).Take(max).ToList();
        }
    }

calling like this
async void LocationsFunc(string search)
        {
            if (IsLoading)
                return;

            IsLoading = true;
            List<Location> locs = await Locator.GetCitiesAsync(search);
            IsLoading = false;
}

A first chance exception of type 'System.Exception' occurred in System.Windows.dll
A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll
A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll
A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll

try/catch does not help, wherever it is.
I don't know, what's the matter? What difference does it make to him? After all, a new WebClient instance is being created.
In the callback/event style, everything works.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mayorovp, 2015-02-27
@mayorovp

And even that doesn't work?

if (IsLoading)
            return;
try {
            IsLoading = true;
            List<Location> locs = await Locator.GetCitiesAsync(search);
} catch (Exception ex) {
            Debug.Break();
            // тут бы MessageBox какой-нибудь или записать в лог
} finally {
            IsLoading = false;
}

L
llexus, 2015-02-27
@llexus

It's very strange, but now the code from my initial post is working.. yesterday I fixed it by replacing the line
List locs = await Locator.GetCitiesAsync(search);
on
foreach (Location loc in await Locator.GetCitiesAsync(search))
...
and it worked. Apparently the whole point was to bind the Task to some local variable. try/catch wrapped everything and everything, but nothing was caught, but just fell .. I didn’t try to restart the emulator, maybe it was somewhere in the depths of the system ..

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question