コード例 #1
0
        private static async Task <string> BuildAtomFeed(
            DateTimeOffset lastUpdate,
            PagedQueryable <ParcelSyndicationQueryResult> pagedParcels,
            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 atomConfiguration        = AtomFeedConfigurationBuilder.CreateFrom(syndicationConfiguration, lastUpdate);

                await writer.WriteDefaultMetadata(atomConfiguration);

                var parcels = pagedParcels.Items.ToList();

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

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

                foreach (var parcel in pagedParcels.Items)
                {
                    await writer.WriteParcel(responseOptions, formatter, syndicationConfiguration["Category"], parcel);
                }

                xmlWriter.Flush();
            }

            return(sw.ToString());
        }
コード例 #2
0
        private static async Task <string> BuildAtomFeed(
            PagedQueryable <ParcelSyndicationQueryResult> pagedParcels,
            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 = BuildVolgendeUri(pagedParcels.PaginationInfo, syndicationConfiguration["NextUri"]);
                if (nextUri != null)
                {
                    await writer.Write(new SyndicationLink(nextUri, "next"));
                }

                foreach (var parcel in pagedParcels.Items)
                {
                    await writer.WriteParcel(responseOptions, formatter, syndicationConfiguration["Category"], parcel);
                }

                xmlWriter.Flush();
            }

            return(sw.ToString());
        }