Skip to content

Commit

Permalink
Add example of IAsyncEnumerable version of RSS reader
Browse files Browse the repository at this point in the history
  • Loading branch information
dodyg committed Aug 10, 2019
1 parent faad021 commit e68f4e9
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 282 samples for ASP.NET Core 2.1, 2.2 and 3.0-preview-7 fundamentals (updated almost daily - except during Summer)
# 283 samples for ASP.NET Core 2.1, 2.2 and 3.0-preview-7 fundamentals (updated almost daily - except during Summer)

If you are studying ASP.NET Core, I am lurking on this **[Gitter Channel](https://gitter.im/DotNetStudyGroup/aspnetcore)**.

Expand All @@ -21,7 +21,7 @@ ASP.NET Core API Browser is also very [handy](https://docs.microsoft.com/en-us/d
| ------- | ------- | ------- |
| [ASP.NET Core 3.0](/projects/3-0) | 37 | 3.0-preview-7 |
| [Blazor Client Side (Web Assembly)](/projects/blazor/README.md) | 17 | 3.0-preview-7 |
| [Blazor Server Side](/projects/blazor-ss) | 6 | 3.0-preview-7|
| [Blazor Server Side](/projects/blazor-ss) | 7 | 3.0-preview-7|
| [ASP.NET Core MVC](/projects/mvc/README.md) | 45 | 2.1 |
| [ASP.NET Core Razor Pages](/projects/razor-pages/README.md) | 4| 2.2 |
| [ASP.NET Core SignalR](/projects/signalr/README.md) |1| 2.1 |
Expand Down
6 changes: 5 additions & 1 deletion projects/blazor-ss/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Server Size Blazor (6)
# Server Size Blazor (7)

This is an amazing piece of technology where your interactive web UI is handled via C# and streamed back and forth using web socket via SignalR.

Expand All @@ -21,6 +21,10 @@ All the samples in this section runs on SSL. If you have not gotten your local s

This sample demonstrates that you can use normal server side packages with your Razor Component as it is a truly server side system. This sample uses `Microsoft.SyndicationFeed.ReaderWriter` package to parse an external RSS feed and display it.

* [Rss Reader - 2](RssReader-2)

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.

* [Js Integration](JsIntegration)

This sample shows how to access JavaScript functions available at `windows` global scope.
Expand Down
7 changes: 7 additions & 0 deletions projects/blazor-ss/RssReader-2/Components/App.razor
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>
4 changes: 4 additions & 0 deletions projects/blazor-ss/RssReader-2/Components/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@page "/"

<RssBox></RssBox>

43 changes: 43 additions & 0 deletions projects/blazor-ss/RssReader-2/Components/Pages/RssBox.razor
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();
}
}
}
6 changes: 6 additions & 0 deletions projects/blazor-ss/RssReader-2/Components/_Imports.razor
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
21 changes: 21 additions & 0 deletions projects/blazor-ss/RssReader-2/Pages/Index.cshtml
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>
3 changes: 3 additions & 0 deletions projects/blazor-ss/RssReader-2/Pages/_ViewImports.cshtml
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
61 changes: 61 additions & 0 deletions projects/blazor-ss/RssReader-2/Program.cs
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);
});
}
}
3 changes: 3 additions & 0 deletions projects/blazor-ss/RssReader-2/README.md
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.
18 changes: 18 additions & 0 deletions projects/blazor-ss/RssReader-2/RssReader.csproj
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>
46 changes: 46 additions & 0 deletions projects/blazor-ss/RssReader-2/Services/RssNews.cs
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.
25 changes: 25 additions & 0 deletions projects/blazor-ss/RssReader-2/wwwroot/index.html
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>

0 comments on commit e68f4e9

Please sign in to comment.