Skip to content

Commit 5087657

Browse files
authored
Update BlazorWasm3.2P1Updates (dotnet#16765)
1 parent 99e4b60 commit 5087657

File tree

7 files changed

+111
-42
lines changed

7 files changed

+111
-42
lines changed

aspnetcore/blazor/common/samples/3.x/BlazorWebAssemblySample/BlazorSample.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.1.0-preview4.19579.2" />
10-
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.1.0-preview4.19579.2" PrivateAssets="all" />
11-
<PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.1.0-preview4.19579.2" />
12-
<PackageReference Include="Microsoft.AspNetCore.Blazor.DevServer" Version="3.1.0-preview4.19579.2" PrivateAssets="all" />
9+
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.2.0-preview1.20073.1" />
10+
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.2.0-preview1.20073.1" PrivateAssets="all" />
11+
<PackageReference Include="Microsoft.AspNetCore.Blazor.DevServer" Version="3.2.0-preview1.20073.1" PrivateAssets="all" />
12+
<PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.2.0-preview1.20073.1" />
1313
</ItemGroup>
1414

1515
</Project>
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
using Microsoft.AspNetCore.Blazor.Hosting;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using System.Text;
5+
using Microsoft.AspNetCore.Blazor.Hosting;
6+
using Microsoft.Extensions.DependencyInjection;
27

38
namespace BlazorSample
49
{
510
public class Program
611
{
7-
public static void Main(string[] args)
12+
public static async Task Main(string[] args)
813
{
9-
CreateHostBuilder(args).Build().Run();
10-
}
14+
var builder = WebAssemblyHostBuilder.CreateDefault(args);
15+
builder.RootComponents.Add<App>("app");
1116

12-
public static IWebAssemblyHostBuilder CreateHostBuilder(string[] args) =>
13-
BlazorWebAssemblyHost.CreateDefaultBuilder()
14-
.UseBlazorStartup<Startup>();
17+
await builder.Build().RunAsync();
18+
}
1519
}
1620
}

aspnetcore/blazor/common/samples/3.x/BlazorWebAssemblySample/Startup.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

aspnetcore/blazor/dependency-injection.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: See how Blazor apps can inject services into components.
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 01/08/2020
8+
ms.date: 01/29/2020
99
no-loc: [Blazor, SignalR]
1010
uid: blazor/dependency-injection
1111
---
@@ -36,6 +36,69 @@ A custom service provider doesn't automatically provide the default services lis
3636

3737
## Add services to an app
3838

39+
### Blazor WebAssembly
40+
41+
Configure services for the app's service collection in the `Main` method of *Program.cs*. In the following example, the `MyDependency` implementation is registered for `IMyDependency`:
42+
43+
```csharp
44+
public class Program
45+
{
46+
public static async Task Main(string[] args)
47+
{
48+
var builder = WebAssemblyHostBuilder.CreateDefault(args);
49+
builder.Services.AddSingleton<IMyDependency, MyDependency>();
50+
builder.RootComponents.Add<App>("app");
51+
52+
await builder.Build().RunAsync();
53+
}
54+
}
55+
```
56+
57+
Once the host is built, services can be accessed from the root DI scope before any components are rendered. This can be useful for running initialization logic before rendering content:
58+
59+
```csharp
60+
public class Program
61+
{
62+
public static async Task Main(string[] args)
63+
{
64+
var builder = WebAssemblyHostBuilder.CreateDefault(args);
65+
builder.Services.AddSingleton<WeatherService>();
66+
builder.RootComponents.Add<App>("app");
67+
68+
var host = builder.Build();
69+
70+
var weatherService = host.Services.GetRequiredService<WeatherService>();
71+
await weatherService.InitializeWeatherAsync();
72+
73+
await host.RunAsync();
74+
}
75+
}
76+
```
77+
78+
The host also provides a central configuration instance for the app. Building on the preceding example, the weather service's URL is passed from a default configuration source (for example, *appsettings.json*) to `InitializeWeatherAsync`:
79+
80+
```csharp
81+
public class Program
82+
{
83+
public static async Task Main(string[] args)
84+
{
85+
var builder = WebAssemblyHostBuilder.CreateDefault(args);
86+
builder.Services.AddSingleton<WeatherService>();
87+
builder.RootComponents.Add<App>("app");
88+
89+
var host = builder.Build();
90+
91+
var weatherService = host.Services.GetRequiredService<WeatherService>();
92+
await weatherService.InitializeWeatherAsync(
93+
host.Configuration["WeatherServiceUrl"]);
94+
95+
await host.RunAsync();
96+
}
97+
}
98+
```
99+
100+
### Blazor Server
101+
39102
After creating a new app, examine the `Startup.ConfigureServices` method:
40103

41104
```csharp
@@ -54,6 +117,8 @@ public void ConfigureServices(IServiceCollection services)
54117
}
55118
```
56119

120+
### Service lifetime
121+
57122
Services can be configured with the lifetimes shown in the following table.
58123

59124
| Lifetime | Description |

aspnetcore/blazor/hosting-models.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
88
ms.date: 12/18/2019
9-
no-loc: [Blazor, SignalR, blazor.webassembly.js]
9+
no-loc: [Blazor, SignalR]
1010
uid: blazor/hosting-models
1111
---
1212
# ASP.NET Core Blazor hosting models

aspnetcore/blazor/templates.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn about ASP.NET Core Blazor app templates and Blazor project st
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 12/18/2019
8+
ms.date: 01/29/2020
99
no-loc: [Blazor, SignalR]
1010
uid: blazor/templates
1111
---
@@ -28,16 +28,20 @@ For step-by-step instructions on creating a Blazor app from a template, see <xre
2828

2929
The following files and folders make up a Blazor app generated from a Blazor template:
3030

31-
* *Program.cs* &ndash; The app's entry point that sets up the ASP.NET Core [host](xref:fundamentals/host/generic-host). The code in this file is common to all ASP.NET Core apps generated from ASP.NET Core templates.
31+
* *Program.cs* &ndash; The app's entry point that sets up the:
3232

33-
* *Startup.cs* &ndash; Contains the app's startup logic. The `Startup` class defines two methods:
33+
* ASP.NET Core [host](xref:fundamentals/host/generic-host) (Blazor Server)
34+
* WebAssembly host (Blazor WebAssembly) &ndash; The code in this file is unique to apps created from the Blazor WebAssembly template (`blazorwasm`).
35+
* The `App` component, which is the root component of the app, is specified as the `app` DOM element to the `Add` method.
36+
* Services can be configured with the `ConfigureServices` method on the host builder (for example, `builder.Services.AddSingleton<IMyDependency, MyDependency>();`).
37+
* Configuration can be supplied via the host builder (`builder.Configuration`).
38+
39+
* *Startup.cs* (Blazor Server) &ndash; Contains the app's startup logic. The `Startup` class defines two methods:
3440

3541
* `ConfigureServices` &ndash; Configures the app's [dependency injection (DI)](xref:fundamentals/dependency-injection) services. In Blazor Server apps, services are added by calling <xref:Microsoft.Extensions.DependencyInjection.ComponentServiceCollectionExtensions.AddServerSideBlazor*>, and the `WeatherForecastService` is added to the service container for use by the example `FetchData` component.
3642
* `Configure` &ndash; Configures the app's request handling pipeline:
37-
* Blazor WebAssembly &ndash; Adds the `App` component (specified as the `app` DOM element to the `AddComponent` method), which is the root component of the app.
38-
* Blazor Server
39-
* <xref:Microsoft.AspNetCore.Builder.ComponentEndpointRouteBuilderExtensions.MapBlazorHub*> is called to set up an endpoint for the real-time connection with the browser. The connection is created with [SignalR](xref:signalr/introduction), which is a framework for adding real-time web functionality to apps.
40-
* [MapFallbackToPage("/_Host")](xref:Microsoft.AspNetCore.Builder.RazorPagesEndpointRouteBuilderExtensions.MapFallbackToPage*) is called to set up the root page of the app (*Pages/_Host.cshtml*) and enable navigation.
43+
* <xref:Microsoft.AspNetCore.Builder.ComponentEndpointRouteBuilderExtensions.MapBlazorHub*> is called to set up an endpoint for the real-time connection with the browser. The connection is created with [SignalR](xref:signalr/introduction), which is a framework for adding real-time web functionality to apps.
44+
* [MapFallbackToPage("/_Host")](xref:Microsoft.AspNetCore.Builder.RazorPagesEndpointRouteBuilderExtensions.MapFallbackToPage*) is called to set up the root page of the app (*Pages/_Host.cshtml*) and enable navigation.
4145

4246
* *wwwroot/index.html* (Blazor WebAssembly) &ndash; The root page of the app implemented as an HTML page:
4347
* When any page of the app is initially requested, this page is rendered and returned in the response.

aspnetcore/security/blazor/index.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn about Blazor authentication and authorization scenarios.
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 12/18/2019
8+
ms.date: 01/29/2020
99
no-loc: [Blazor, SignalR]
1010
uid: security/blazor/index
1111
---
@@ -183,13 +183,26 @@ namespace BlazorSample.Services
183183
}
184184
```
185185

186-
The `CustomAuthStateProvider` service is registered in `Startup.ConfigureServices`:
186+
In a Blazor WebAssembly app, the `CustomAuthStateProvider` service is registered in `Main` of *Program.cs*:
187187

188188
```csharp
189-
// using Microsoft.AspNetCore.Components.Authorization;
190-
// using BlazorSample.Services;
189+
using Microsoft.AspNetCore.Blazor.Hosting;
190+
using Microsoft.AspNetCore.Components.Authorization;
191+
using Microsoft.Extensions.DependencyInjection;
192+
using BlazorSample.Services;
193+
194+
public class Program
195+
{
196+
public static async Task Main(string[] args)
197+
{
198+
var builder = WebAssemblyHostBuilder.CreateDefault(args);
199+
builder.Services.AddScoped<AuthenticationStateProvider,
200+
CustomAuthStateProvider>();
201+
builder.RootComponents.Add<App>("app");
191202

192-
services.AddScoped<AuthenticationStateProvider, CustomAuthStateProvider>();
203+
await builder.Build().RunAsync();
204+
}
205+
}
193206
```
194207

195208
Using the `CustomAuthStateProvider`, all users are authenticated with the username `mrfibuli`.

0 commit comments

Comments
 (0)