Method call Async TaskCompletionSource twice

found this method with await to call the async method with a callback, I used it in the Argotic RSS reader:

 var tcs = new TaskCompletionSource<string>(); EventHandler<SyndicationResourceLoadedEventArgs> feedReaderOnLoaded = null; feedReaderOnLoaded = (sender, args) => { feedReader.Loaded -= feedReaderOnLoaded; tcs.SetResult(""); // Needed so the await completes }; feedReader.Loaded += feedReaderOnLoaded; feedReader.LoadAsync(new Uri(feed.Url), new object()); await tcs.Task; // Result is put in a property, rather than returned from the method var items = feedReader.Channel.Items; 

So this works well, and I get my items.

I noticed in Fiddler that there are two calls to the RSS feed. After executing the code in the debugger, it is called once on LoadAsync and again on await tcs.Task . What to do to exclude one of the calls?

UPDATE This is a console application project, which may be here , that demonstrates this behavior.

UPDATE I changed the way I use the Argotic library to load an RSS feed using HttpClient , and then pass the contents to Argotic as a string that now produces only one call. Anyway, I would like to know why this was caused twice, if anyone has any ideas.

+4
source share
1 answer

This appears to be a bug / feature in the Async API for the feed reader .

It runs an asynchronous WebRequest to receive the feed. However, when it handles the completion of WebRequest, it creates an XmlReader for the response, but actually uses: SyndicationEncodingUtility.CreateSafeNavigator(Url, new WebRequestOptions(), null) which reads it again (synchronously) from the source URL.

The feed itself will be cached a second time, so it does not cause a lot of overhead.

Think that this can be CreateSafeNavigator passing the response stream to CreateSafeNavigator , but perhaps reading the response stream may also benefit from asynchronous use.

+1
source

Source: https://habr.com/ru/post/1433007/


All Articles