コード例 #1
0
        private static async Task <string> BuildAtomFeed(
            DateTimeOffset lastFeedUpdate,
            PagedQueryable <AddressSyndicationQueryResult> pagedAddresses,
            IOptions <ResponseOptions> responseOptions,
            IConfiguration configuration)
        {
            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            using (var xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings {
                Async = true, Indent = true, Encoding = sw.Encoding
            }))
            {
                var formatter = new AtomFormatter(null, xmlWriter.Settings)
                {
                    UseCDATA = true
                };
                var writer = new AtomFeedWriter(xmlWriter, null, formatter);
                var syndicationConfiguration = configuration.GetSection("Syndication");
                var atomFeedConfig           = AtomFeedConfigurationBuilder.CreateFrom(syndicationConfiguration, lastFeedUpdate);

                await writer.WriteDefaultMetadata(atomFeedConfig);

                var addresses = pagedAddresses.Items.ToList();

                var nextFrom = addresses.Any()
                    ? addresses.Max(x => x.Position) + 1
                    : (long?)null;

                var nextUri = BuildNextSyncUri(pagedAddresses.PaginationInfo.Limit, nextFrom, syndicationConfiguration["NextUri"]);
                if (nextUri != null)
                {
                    await writer.Write(new SyndicationLink(nextUri, "next"));
                }

                foreach (var address in addresses)
                {
                    await writer.WriteAddress(responseOptions, formatter, syndicationConfiguration["Category"], address);
                }

                xmlWriter.Flush();
            }

            return(sw.ToString());
        }
コード例 #2
0
        private static async Task <string> BuildAtomFeed(
            PagedQueryable <AddressSyndicationQueryResult> pagedAddresses,
            IOptions <ResponseOptions> responseOptions,
            IConfiguration configuration)
        {
            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            using (var xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings {
                Async = true, Indent = true, Encoding = sw.Encoding
            }))
            {
                var formatter = new AtomFormatter(null, xmlWriter.Settings)
                {
                    UseCDATA = true
                };
                var writer = new AtomFeedWriter(xmlWriter, null, formatter);
                var syndicationConfiguration = configuration.GetSection("Syndication");

                await writer.WriteDefaultMetadata(
                    syndicationConfiguration["Id"],
                    syndicationConfiguration["Title"],
                    Assembly.GetEntryAssembly().GetName().Version.ToString(),
                    new Uri(syndicationConfiguration["Self"]),
                    syndicationConfiguration.GetSection("Related").GetChildren().Select(c => c.Value).ToArray());

                var nextUri = pagedAddresses.PaginationInfo.BuildNextUri(syndicationConfiguration["NextUri"]);
                if (nextUri != null)
                {
                    await writer.Write(new SyndicationLink(nextUri, "next"));
                }

                foreach (var address in pagedAddresses.Items)
                {
                    await writer.WriteAddress(responseOptions, formatter, syndicationConfiguration["Category"], address);
                }

                xmlWriter.Flush();
            }

            return(sw.ToString());
        }