Skip to content

Commit e9810a1

Browse files
committed
#15 add blazor and configure to use with current SignalR host
1 parent a7b124c commit e9810a1

32 files changed

+1240
-2
lines changed

netcorekit.sln

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreKit.Samples.SignalRN
5656
EndProject
5757
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreKit.Samples.Contracts", "samples\Contracts\NetCoreKit.Samples.Contracts.csproj", "{C7C5DA44-023A-4138-8F13-D7FA2EB58F74}"
5858
EndProject
59+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreKit.Samples.WebNotifier", "samples\WebNotifier\NetCoreKit.Samples.WebNotifier.csproj", "{02E8D785-5013-47B2-BFC6-371874C8BA64}"
60+
EndProject
5961
Global
6062
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6163
Debug|Any CPU = Debug|Any CPU
@@ -192,6 +194,14 @@ Global
192194
{C7C5DA44-023A-4138-8F13-D7FA2EB58F74}.Release|Any CPU.Build.0 = Release|Any CPU
193195
{C7C5DA44-023A-4138-8F13-D7FA2EB58F74}.Release|x86.ActiveCfg = Release|Any CPU
194196
{C7C5DA44-023A-4138-8F13-D7FA2EB58F74}.Release|x86.Build.0 = Release|Any CPU
197+
{02E8D785-5013-47B2-BFC6-371874C8BA64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
198+
{02E8D785-5013-47B2-BFC6-371874C8BA64}.Debug|Any CPU.Build.0 = Debug|Any CPU
199+
{02E8D785-5013-47B2-BFC6-371874C8BA64}.Debug|x86.ActiveCfg = Debug|Any CPU
200+
{02E8D785-5013-47B2-BFC6-371874C8BA64}.Debug|x86.Build.0 = Debug|Any CPU
201+
{02E8D785-5013-47B2-BFC6-371874C8BA64}.Release|Any CPU.ActiveCfg = Release|Any CPU
202+
{02E8D785-5013-47B2-BFC6-371874C8BA64}.Release|Any CPU.Build.0 = Release|Any CPU
203+
{02E8D785-5013-47B2-BFC6-371874C8BA64}.Release|x86.ActiveCfg = Release|Any CPU
204+
{02E8D785-5013-47B2-BFC6-371874C8BA64}.Release|x86.Build.0 = Release|Any CPU
195205
EndGlobalSection
196206
GlobalSection(SolutionProperties) = preSolution
197207
HideSolutionNode = FALSE
@@ -214,6 +224,7 @@ Global
214224
{7AE8F281-2C86-4CF5-80CF-FCCE940E5DC3} = {92D6FD73-B95C-4CBB-A48E-047672EB4E77}
215225
{9894E768-600D-452E-9F9E-6612D7AA6421} = {92D6FD73-B95C-4CBB-A48E-047672EB4E77}
216226
{C7C5DA44-023A-4138-8F13-D7FA2EB58F74} = {92D6FD73-B95C-4CBB-A48E-047672EB4E77}
227+
{02E8D785-5013-47B2-BFC6-371874C8BA64} = {92D6FD73-B95C-4CBB-A48E-047672EB4E77}
217228
EndGlobalSection
218229
GlobalSection(ExtensibilityGlobals) = postSolution
219230
SolutionGuid = {B9325AE8-21A8-4D46-9B04-38BD2BC35C0F}

samples/Contracts/NetCoreKit.Samples.Contracts.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.1</TargetFramework>
4+
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>

samples/SignalRNotifier/Startup.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
2020
services.AddSignalR();
2121
services.AddSingleton<IHostedService, ProjectHostService>();
2222

23+
services.AddCors(options => options.AddPolicy("CorsPolicy", builder =>
24+
{
25+
builder
26+
.AllowAnyMethod()
27+
.AllowAnyHeader()
28+
.AllowAnyOrigin()
29+
.AllowCredentials()
30+
.SetIsOriginAllowedToAllowWildcardSubdomains();
31+
}));
32+
2333
return BuildServiceProvider(services);
2434
}
2535

@@ -30,6 +40,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
3040
app.UseDeveloperExceptionPage();
3141
}
3242

43+
app.UseCors("CorsPolicy");
44+
3345
app.UseSignalR(routes =>
3446
{
3547
routes.MapHub<ProjectHub>("/project");

samples/WebNotifier/App.cshtml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!--
2+
Configuring this here is temporary. Later we'll move the app config
3+
into Program.cs, and it won't be necessary to specify AppAssembly.
4+
-->
5+
<Router AppAssembly=typeof(Program).Assembly />
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<RunCommand>dotnet</RunCommand>
6+
<RunArguments>blazor serve</RunArguments>
7+
<LangVersion>7.3</LangVersion>
8+
<AssemblyName>WebNotifier</AssemblyName>
9+
<RootNamespace>WebNotifier</RootNamespace>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Blazor.Extensions.SignalR" Version="0.1.5" />
14+
<PackageReference Include="Microsoft.AspNetCore.Blazor.Browser" Version="0.5.1" />
15+
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="0.5.1" />
16+
<DotNetCliToolReference Include="Microsoft.AspNetCore.Blazor.Cli" Version="0.5.1" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@page "/counter"
2+
3+
<h1>Counter</h1>
4+
5+
<p>Current count: @currentCount</p>
6+
7+
<button class="btn btn-primary" onclick="@IncrementCount">Click me</button>
8+
9+
@functions {
10+
int currentCount = 0;
11+
12+
void IncrementCount()
13+
{
14+
currentCount++;
15+
}
16+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
@page "/fetchdata"
2+
@inject HttpClient Http
3+
4+
<h1>Weather forecast</h1>
5+
6+
<p>This component demonstrates fetching data from the server.</p>
7+
8+
@if (forecasts == null)
9+
{
10+
<p><em>Loading...</em></p>
11+
}
12+
else
13+
{
14+
<table class="table">
15+
<thead>
16+
<tr>
17+
<th>Date</th>
18+
<th>Temp. (C)</th>
19+
<th>Temp. (F)</th>
20+
<th>Summary</th>
21+
</tr>
22+
</thead>
23+
<tbody>
24+
@foreach (var forecast in forecasts)
25+
{
26+
<tr>
27+
<td>@forecast.Date.ToShortDateString()</td>
28+
<td>@forecast.TemperatureC</td>
29+
<td>@forecast.TemperatureF</td>
30+
<td>@forecast.Summary</td>
31+
</tr>
32+
}
33+
</tbody>
34+
</table>
35+
}
36+
37+
@functions {
38+
WeatherForecast[] forecasts;
39+
40+
protected override async Task OnInitAsync()
41+
{
42+
forecasts = await Http.GetJsonAsync<WeatherForecast[]>("sample-data/weather.json");
43+
}
44+
45+
class WeatherForecast
46+
{
47+
public DateTime Date { get; set; }
48+
public int TemperatureC { get; set; }
49+
public int TemperatureF { get; set; }
50+
public string Summary { get; set; }
51+
}
52+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@page "/"
2+
3+
<h1>Hello, world!</h1>
4+
5+
Welcome to your new app.
6+
7+
<SurveyPrompt Title="How is Blazor working for you?" />
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@using Blazor.Extensions
2+
@page "/notification"
3+
@inherits WebNotifier.Pages.NotificationComponent
4+
5+
<h1>Notification.</h1>
6+
7+
<ul id="message-list">
8+
@foreach (var msg in Messages)
9+
{
10+
<li>@msg</li>
11+
}
12+
</ul>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using Blazor.Extensions;
4+
using Microsoft.AspNetCore.Blazor.Components;
5+
6+
namespace WebNotifier.Pages
7+
{
8+
public class NotificationComponent : BlazorComponent
9+
{
10+
private HubConnection _connection;
11+
public List<string> Messages { get; set; } = new List<string>();
12+
13+
protected override async Task OnInitAsync()
14+
{
15+
_connection = new HubConnectionBuilder()
16+
.WithUrl("https://localhost:44398/project",
17+
opt =>
18+
{
19+
opt.LogLevel = SignalRLogLevel.Trace;
20+
opt.Transport = HttpTransportType.WebSockets;
21+
})
22+
.Build();
23+
24+
_connection.On<object>("projectCreatedNotify", HandleProjectCreated);
25+
_connection.On<object>("taskAddedToProjectNotify", HandleTaskCreated);
26+
27+
_connection.OnClose(exc => Task.CompletedTask);
28+
await _connection.StartAsync();
29+
}
30+
31+
private Task HandleProjectCreated(object msg)
32+
{
33+
Messages.Add(msg.ToString());
34+
StateHasChanged();
35+
return Task.CompletedTask;
36+
}
37+
38+
private Task HandleTaskCreated(object msg)
39+
{
40+
Messages.Add(msg.ToString());
41+
StateHasChanged();
42+
return Task.CompletedTask;
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)