Home > Blockchain >  What is the best way to return a list for an ASync method?
What is the best way to return a list for an ASync method?

Time:06-03

I'm writing an RSS reader using this example https://github.com/dotnet/SyndicationFeedReaderWriter

I want to record all ISyndicationItem's to return as a list. The compiler error I'm getting is "Error CS1983 The return type of an async method must be void, Task, Task, a task-like type, IAsyncEnumerable, or IAsyncEnumerator" What is the most ideal way to do this?

using Microsoft.SyndicationFeed;
using Microsoft.SyndicationFeed.Rss;
using System.Collections.Generic;
using System.Xml;

static async List<ISyndicationItem> ReadRSSFeed(string inputUri)
        {
            var itemList = new List<ISyndicationItem>();
            using (var xmlReader = XmlReader.Create(inputUri, new XmlReaderSettings() { Async = true }))
            {
                var feedReader = new RssFeedReader(xmlReader);

                while (await feedReader.Read())
                {
                    switch (feedReader.ElementType)
                    {
                        // Read category
                        case SyndicationElementType.Category:
                            ISyndicationCategory category = await feedReader.ReadCategory();
                            break;

                        // Read Image
                        case SyndicationElementType.Image:
                            ISyndicationImage image = await feedReader.ReadImage();
                            break;

                        // Read Item
                        case SyndicationElementType.Item:
                            ISyndicationItem item = await feedReader.ReadItem();
                            itemList.Add(item);
                            break;

                        // Read link
                        case SyndicationElementType.Link:
                            ISyndicationLink link = await feedReader.ReadLink();
                            break;

                        // Read Person
                        case SyndicationElementType.Person:
                            ISyndicationPerson person = await feedReader.ReadPerson();
                            break;

                        // Read content
                        default:
                            ISyndicationContent content = await feedReader.ReadContent();
                            break;
                    }
                }
            }
            return itemList;
        }

CodePudding user response:

Just return Task<T> instead of T. Inside the method you still return an instance of T.

So should be:

static async Task<List<ISyndicationItem>> ReadRSSFeed(string inputUri)
  • Related