Skip to content

Commit a7f9806

Browse files
authored
Add Blazor WASM .NET on Web Workers sample app (#635)
1 parent d7a4b35 commit a7f9806

52 files changed

Lines changed: 882 additions & 1 deletion

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Router AppAssembly="@typeof(App).Assembly" NotFoundPage="typeof(Pages.NotFound)">
2+
<Found Context="routeData">
3+
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
4+
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
5+
</Found>
6+
</Router>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="10.0.0" />
12+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="10.0.0" PrivateAssets="all" />
13+
<PackageReference Include="QRCoder" Version="1.7.0" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<SupportedPlatform Include="browser" />
18+
</ItemGroup>
19+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 18
4+
VisualStudioVersion = 18.3.11218.70 d18.3
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorWasm", "BlazorWasm.csproj", "{C130AD78-5381-25F8-31BC-8D6ABC8C689E}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{C130AD78-5381-25F8-31BC-8D6ABC8C689E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C130AD78-5381-25F8-31BC-8D6ABC8C689E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C130AD78-5381-25F8-31BC-8D6ABC8C689E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C130AD78-5381-25F8-31BC-8D6ABC8C689E}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {30A17113-5A5F-430E-BF98-304E5F74DFBE}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// dummy file to let blazor handle Client.razor.js file loading
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Runtime.InteropServices.JavaScript;
5+
using System.Runtime.Versioning;
6+
7+
[SupportedOSPlatform("browser")]
8+
public partial class Client
9+
{
10+
private static bool workerDotnetStarted;
11+
12+
public static async Task InitClient()
13+
{
14+
if (workerDotnetStarted)
15+
{
16+
return;
17+
}
18+
19+
workerDotnetStarted = true;
20+
21+
await JSHost.ImportAsync(
22+
moduleName: nameof(Client),
23+
moduleUrl: $"../Clients/Client.razor.js");
24+
}
25+
26+
[JSImport("generateQR", nameof(Client))]
27+
public static partial Task<string> GenerateQR(string text, int size);
28+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
const pendingRequests = {};
5+
let pendingRequestId = 0;
6+
7+
const dotnetWorker = new Worker('./Workers/QRGenerator.razor.js', { type: "module" });
8+
dotnetWorker.addEventListener('message', async function (e) {
9+
switch (e.data.command) {
10+
case "response":
11+
if (!e.data.requestId) {
12+
console.error("No requestId in response from worker");
13+
}
14+
const request = pendingRequests[e.data.requestId];
15+
delete pendingRequests[e.data.requestId];
16+
if (e.data.error) {
17+
request.reject(new Error(e.data.error));
18+
}
19+
request.resolve(e.data.result);
20+
break;
21+
default:
22+
console.log('Worker said: ', e.data);
23+
break;
24+
}
25+
}, false);
26+
27+
function sendRequestToWorker(request) {
28+
pendingRequestId++;
29+
const promise = new Promise((resolve, reject) => {
30+
pendingRequests[pendingRequestId] = { resolve, reject };
31+
});
32+
dotnetWorker.postMessage({
33+
...request,
34+
requestId: pendingRequestId
35+
});
36+
return promise;
37+
}
38+
39+
export async function generateQR(text, size) {
40+
const response = await sendRequestToWorker({
41+
command: "generateQR",
42+
text: text,
43+
size: size
44+
});
45+
const blob = new Blob([response], { type: 'image/png' });
46+
return URL.createObjectURL(blob);
47+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@namespace Components
2+
3+
@if (this.isVisible)
4+
{
5+
<div class="modal d-block">
6+
<div class="modal-dialog">
7+
<div class="modal-content">
8+
<div class="modal-header">
9+
<h5 class="modal-title">@title</h5>
10+
<button type="button" class="close" @onclick="@Hide" data-dismiss="modal" aria-label="Close">
11+
<span aria-hidden="true">×</span>
12+
</button>
13+
</div>
14+
<div class="modal-body">
15+
<p for="message">@message</p>
16+
<div class="text-center">
17+
<button class="btn btn-outline-danger" @onclick="@Hide">Close</button>
18+
</div>
19+
</div>
20+
</div>
21+
</div>
22+
</div>
23+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.AspNetCore.Components;
2+
using System.Runtime.Versioning;
3+
4+
namespace Components;
5+
6+
[SupportedOSPlatform("browser")]
7+
public partial class Popup : ComponentBase
8+
{
9+
private string message = string.Empty;
10+
private string title = string.Empty;
11+
private bool isVisible;
12+
13+
public void Show(string title, string message)
14+
{
15+
Console.WriteLine($"Popup.Show: {title} - {message}");
16+
this.title = title;
17+
this.message = message;
18+
isVisible = true;
19+
InvokeAsync(StateHasChanged);
20+
}
21+
22+
public void Hide()
23+
{
24+
isVisible = false;
25+
InvokeAsync(StateHasChanged);
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@namespace Layout
2+
@inherits LayoutComponentBase
3+
<div class="page">
4+
<div class="sidebar">
5+
<NavMenu />
6+
</div>
7+
8+
<main>
9+
<div class="top-row px-4">
10+
<a href="https://learn.microsoft.com/aspnet/core/" target="_blank">About</a>
11+
</div>
12+
13+
<article class="content px-4">
14+
@Body
15+
</article>
16+
</main>
17+
</div>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
.page {
2+
position: relative;
3+
display: flex;
4+
flex-direction: column;
5+
}
6+
7+
main {
8+
flex: 1;
9+
}
10+
11+
.sidebar {
12+
background-image: linear-gradient(180deg, rgb(5, 39, 103) 0%, #3a0647 70%);
13+
}
14+
15+
.top-row {
16+
background-color: #f7f7f7;
17+
border-bottom: 1px solid #d6d5d5;
18+
justify-content: flex-end;
19+
height: 3.5rem;
20+
display: flex;
21+
align-items: center;
22+
}
23+
24+
.top-row ::deep a, .top-row ::deep .btn-link {
25+
white-space: nowrap;
26+
margin-left: 1.5rem;
27+
text-decoration: none;
28+
}
29+
30+
.top-row ::deep a:hover, .top-row ::deep .btn-link:hover {
31+
text-decoration: underline;
32+
}
33+
34+
.top-row ::deep a:first-child {
35+
overflow: hidden;
36+
text-overflow: ellipsis;
37+
}
38+
39+
@media (max-width: 640.98px) {
40+
.top-row {
41+
justify-content: space-between;
42+
}
43+
44+
.top-row ::deep a, .top-row ::deep .btn-link {
45+
margin-left: 0;
46+
}
47+
}
48+
49+
@media (min-width: 641px) {
50+
.page {
51+
flex-direction: row;
52+
}
53+
54+
.sidebar {
55+
width: 250px;
56+
height: 100vh;
57+
position: sticky;
58+
top: 0;
59+
}
60+
61+
.top-row {
62+
position: sticky;
63+
top: 0;
64+
z-index: 1;
65+
}
66+
67+
.top-row.auth ::deep a:first-child {
68+
flex: 1;
69+
text-align: right;
70+
width: 0;
71+
}
72+
73+
.top-row, article {
74+
padding-left: 2rem !important;
75+
padding-right: 1.5rem !important;
76+
}
77+
}

0 commit comments

Comments
 (0)