forked from dodyg/practical-aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of IAsyncEnumerable version of RSS reader
- Loading branch information
Showing
14 changed files
with
244 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<CascadingAuthenticationState> | ||
<Router AppAssembly="typeof(Startup).Assembly"> | ||
<NotFoundContent> | ||
<p>Sorry, there's nothing at this address.</p> | ||
</NotFoundContent> | ||
</Router> | ||
</CascadingAuthenticationState> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@page "/" | ||
|
||
<RssBox></RssBox> | ||
|
43 changes: 43 additions & 0 deletions
43
projects/blazor-ss/RssReader-2/Components/Pages/RssBox.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
@using Microsoft.SyndicationFeed | ||
@inject RssNews rss | ||
|
||
<h1>RSS News </h1> | ||
|
||
<ul> | ||
@foreach(var n in News) | ||
{ | ||
<li>@((MarkupString)n.Description)</li> | ||
} | ||
</ul> | ||
|
||
@code | ||
{ | ||
List<SyndicationItem> News {get; set;} = new List<SyndicationItem>(); | ||
|
||
protected override async Task OnInitAsync() | ||
{ | ||
var feeds = new string[] | ||
{ | ||
"http://scripting.com/rss.xml", | ||
"https://hnrss.org/jobs", | ||
"https://hnrss.org/newest", | ||
"http://feeds.bbci.co.uk/news/rss.xml", | ||
"http://feeds.bbci.co.uk/news/world/rss.xml", | ||
"http://feeds.bbci.co.uk/news/uk/rss.xml", | ||
"http://feeds.bbci.co.uk/news/business/rss.xml", | ||
"http://feeds.bbci.co.uk/news/politics/rss.xml", | ||
"http://feeds.bbci.co.uk/news/health/rss.xml", | ||
"http://feeds.bbci.co.uk/news/education/rss.xml", | ||
"http://feeds.bbci.co.uk/news/entertainment_and_arts/rss.xml" | ||
}; | ||
|
||
await foreach(var news in rss.GetMultipleNewsAsync(feeds)) | ||
{ | ||
foreach(var n in news) | ||
{ | ||
News.Insert(0, n); | ||
} | ||
this.StateHasChanged(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@using System.Net.Http | ||
@using Microsoft.AspNetCore.Components.Forms | ||
@using Microsoft.AspNetCore.Components.Layouts | ||
@using Microsoft.AspNetCore.Components.Routing | ||
@using Microsoft.JSInterop | ||
@using RssReader.Services |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
@page "/" | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>RSS Reader</title> | ||
<base href="~/" /> | ||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" | ||
asp-fallback-href="css/bootstrap/bootstrap.min.css" | ||
asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" | ||
crossorigin="anonymous" | ||
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"/> | ||
<link href="css/site.css" rel="stylesheet" /> | ||
</head> | ||
<body> | ||
<app></app> | ||
|
||
<script src="_framework/blazor.server.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@using RssReader.Components | ||
@namespace RssReader.Pages | ||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using RssReader.Services; | ||
using RssReader.Components; | ||
|
||
namespace RssReader | ||
{ | ||
public class Startup | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddRazorPages(); | ||
services.AddServerSideBlazor(); | ||
|
||
services.AddSingleton<RssNews>(); | ||
} | ||
|
||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | ||
public void Configure(IApplicationBuilder app, IHostEnvironment env) | ||
{ | ||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
else | ||
{ | ||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. | ||
app.UseHsts(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
app.UseStaticFiles(); | ||
|
||
app.UseRouting(); | ||
app.UseEndpoints(routes => | ||
{ | ||
routes.MapRazorPages(); | ||
routes.MapFallbackToPage("/Index"); | ||
routes.MapBlazorHub<App>("app"); | ||
}); | ||
} | ||
} | ||
|
||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
webBuilder.UseEnvironment(Environments.Development); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Rss Reader | ||
|
||
This version of RSS Reader uses C# 8.0 `IAsyncEnumerable` to process RSS data as they are available. There is an artificial `await Task.Delay(3000);` added to `RssNews.GetNewsAsync` so you can see visually how the UI changes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.0</TargetFramework> | ||
<LangVersion>8.0</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.SyndicationFeed.ReaderWriter" Version="1.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
|
||
<Watch Include="**\*.cshtml" /> | ||
<Watch Include="**\*.razor" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.SyndicationFeed; | ||
using Microsoft.SyndicationFeed.Rss; | ||
using System.Xml; | ||
using System.Text; | ||
namespace RssReader.Services | ||
{ | ||
public class RssNews | ||
{ | ||
public async IAsyncEnumerable<List<SyndicationItem>> GetMultipleNewsAsync(params string[] news) | ||
{ | ||
foreach (var x in news) | ||
{ | ||
yield return await GetNewsAsync(x); | ||
} | ||
} | ||
|
||
public async Task<List<SyndicationItem>> GetNewsAsync(string url) | ||
{ | ||
var items = new List<SyndicationItem>(); | ||
|
||
using (var xmlReader = XmlReader.Create(url, new XmlReaderSettings { Async = true })) | ||
{ | ||
var feedReader = new RssFeedReader(xmlReader); | ||
|
||
while (await feedReader.Read()) | ||
{ | ||
switch (feedReader.ElementType) | ||
{ | ||
case SyndicationElementType.Item: | ||
ISyndicationItem item = await feedReader.ReadItem(); | ||
items.Add(new SyndicationItem(item)); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
|
||
await Task.Delay(3000); | ||
|
||
return items; | ||
} | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<title>Rss Reader</title> | ||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" | ||
crossorigin="anonymous"> | ||
<base href="/" /> | ||
</head> | ||
|
||
<body> | ||
<app>Loading...</app> | ||
|
||
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" | ||
crossorigin="anonymous"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" | ||
crossorigin="anonymous"></script> | ||
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" | ||
crossorigin="anonymous"></script> | ||
<script src="_framework/components.server.js"></script> | ||
</body> | ||
|
||
</html> |