Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Blazor WebAssembly 3.2.0 Preview 5 release now available
1
Blazor WebAssembly 3.2.0 Preview 5 release now available

Daniel

April 23rd, 2020

A new preview update of Blazor WebAssembly is now available! Here’s what’s new in this release:
  • Read configuration during startup

  • Configure HTTP fetch request options

  • Honor existing web.config when publishing

  • Attach tokens to outgoing requests

  • Support for time zones

Get started

To get started with Blazor WebAssembly 3.2.0 Preview 5 install the latest .NET Core 3.1 SDK.

Quote:NOTE: Version 3.1.201 or later of the .NET Core SDK is required to use this Blazor WebAssembly release! Make sure you have the correct .NET Core SDK version by running
Code:
dotnet --version
from a command prompt.

Once you have the appropriate .NET Core SDK installed, run the following command to install the updated Blazor WebAssembly template:

Code:
dotnet new -i Microsoft.AspNetCore.Components.WebAssembly.Templates::3.2.0-preview5.20216.8

If you’re on Windows using Visual Studio, we recommend installing the latest preview of Visual Studio 2019 16.6. For this preview, you should still install the template from the command-line as described above to ensure that the Blazor WebAssembly template shows up correctly in Visual Studio and on the command-line.

That’s it! You can find additional docs and samples on https://blazor.net.

Upgrade an existing project

To upgrade an existing Blazor WebAssembly app from 3.2.0 Preview 4 to 3.2.0 Preview 5:
  • Update all Microsoft.AspNetCore.Components.WebAssembly.* package references to version 3.2.0-preview5.20216.8.

  • Update any Microsoft.AspNetCore.Components.WebAssembly.Runtime package references to version 3.2.0-preview5.20216.1.

  • Remove any calls to set
    Code:
    WebAssemblyHttpMessageHandlerOptions.DefaultCredentials
    and instead call
    Code:
    SetBrowserRequestCredentials
    on individual requests (see “Configure HTTP fetch request options” section below).

  • Remove the
    Code:
    redirect
    parameter from calls to
    Code:
    TryGetToken
    on
    Code:
    AccessTokenResult
    .

You’re all set!

Read configuration during startup

Configuration data is now available during app startup in
Code:
Program.Main
using the
Code:
Configuration
property on
Code:
WebAssemblyHostBuilder
. This property can now be used both to add configuration sources and to access the current configuration data.

You can see this feature in action in the project templates when you enable authentication with Azure AD, Azure AD B2C, or an OpenID Connect provider of your choice. The authentication settings are stored in appsettings.json and then read from configuration when the app starts up:

Program.cs

Code:
public class Program { public static async Task Main(string[] args) { var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.RootComponents.Add<App>("app"); builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); builder.Services.AddOidcAuthentication(options => { // Configure your authentication provider options here. // For more information, see https://aka.ms/blazor-standalone-auth builder.Configuration.Bind("Local", options.ProviderOptions); }); await builder.Build().RunAsync(); } }

appsettings.json

Code:
{ "Local": { "Authority": "https:login.microsoftonline.com/", "ClientId": "33333333-3333-3333-33333333333333333" } }
Configure HTTP fetch request options

HTTP requests issued from a Blazor WebAssembly app using
Code:
HttpClient
are handled using the browser
Code:
fetch
API. In this release, we’ve added a set of extension methods for
Code:
HttpRequestMessage
that configure various
Code:
fetch
related options. These extension methods live in the
Code:
Microsoft.AspNetCore.Components.WebAssembly.Http
namespace:

Code:
HttpRequestMessage
extension method
|
Fetch request property

Code:
SetBrowserRequestCredentials
|
credentials

Code:
SetBrowserRequestCache
|
cache

Code:
SetBrowserRequestMode
|
mode

Code:
SetBrowserRequestIntegrity
|
integrity

You can set additional options using the more generic
Code:
SetBrowserRequestOption
extension method.

The HTTP response is typically buffered in a Blazor WebAssembly app to enable support for sync reads on the response content. To enable support for response streaming, use the
Code:
SetBrowserResponseStreamingEnabled
extension method on the request.

Honor existing web.config when publishing

When publishing a standalone Blazor WebAssembly app, a web.config is automatically generated for the app that handles configuring IIS appropriately. You can now specify your own web.config in the project, which will get used instead of the generated one.

Attach tokens to outgoing requests

Configuring authentication now adds an
Code:
AuthorizationMessageHandler
as a service that can be used with
Code:
HttpClient
to attach access tokens to outgoing requests. Tokens are acquired using the existing
Code:
IAccessTokenProvider
service. If a token cannot be acquired, an
Code:
AccessTokenNotAvailableException
is thrown. This exception has a
Code:
Redirect
method that can be used to navigate the user to the identity provider to acquire a new token. The
Code:
AuthorizationMessageHandler
can be configured with the authorized URLs, scopes, and return URL using the
Code:
ConfigureHandler
method.

For example, you can configure an
Code:
HttpClient
to use the
Code:
AuthorizationMessageHandler
like this:

Code:
builder.Services.AddSingleton(sp => { return new HttpClient(sp.GetRequiredService<AuthorizationMessageHandler>() .ConfigureHandler( new [] { "https://www.example.com/base" }, scopes: new[] {"example.read", "example.write"})) { BaseAddress = new Uri("https://www.example.com/base") }; });

For convenience, a
Code:
BaseAddressAuthorizationMessageHandler
is also included that is preconfigured with the app base address as an authorized URL. The authentication enabled Blazor WebAssembly templates now use IHttpClientFactory to set up an
Code:
HttpClient
with the
Code:
BaseAddressAuthorizationMessageHandler
:

Code:
builder.Services.AddHttpClient("BlazorWithIdentityApp1.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)) .AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>(); // Supply HttpClient instances that include access tokens when making requests to the server project builder.Services.AddTransient(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("BlazorWithIdentityApp1.ServerAPI"));

You can use the configured
Code:
HttpClient
to make authorized requests using a simple try-catch pattern. For example, here’s the updated code in the
Code:
FetchData
component for requesting the weather forecast data:

Code:
protected override async Task OnInitializedAsync() { try { forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast"); } catch (AccessTokenNotAvailableException exception) { exception.Redirect(); } }

Alternatively, you can simplify things even further by defining a strongly-typed client that handles all of the HTTP and token acquisition concerns within a single class:

WeatherClient.cs

Code:
public class WeatherClient { private readonly HttpClient httpClient; public WeatherClient(HttpClient httpClient) { this.httpClient = httpClient; } public async Task<IEnumerable<WeatherForecast>> GetWeatherForeacasts() { IEnumerable<WeatherForecast> forecasts = new WeatherForecast[0]; try { forecasts = await httpClient.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast"); } catch (AccessTokenNotAvailableException exception) { exception.Redirect(); } return forecasts; } }

Program.cs

Code:
builder.Services.AddHttpClient<WeatherClient>(client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)) .AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();

FetchData.razor

Code:
protected override async Task OnInitializedAsync() { forecasts = await WeatherClient.GetWeatherForeacasts(); }
Support for time zones

Blazor now infers the user’s time zone and uses it in date and time calculations. In addition, APIs on
Code:
System.TimeZoneInfo
that previously returned incomplete results now report correct results.

Help improve the Blazor docs!

Thank you everyone who has taken the time to give feedback on how we can best improve the Blazor docs!

If you haven’t already, please join in with helping us improve the docs by doing the following:
  • As you read the Blazor docs, let us know where we should focus our efforts by telling us if you find a topic helpful or not using the helpfulness widget at the top of each doc page:

  • Use the Feedback section at the bottom of each doc page to let us know when a particular topic is unclear, inaccurate, or incomplete.

  • Comment on our Improve the Blazor docs GitHub issue with your suggestions for new content and ways to improve the existing content.

Feedback

We hope you enjoy the new features in this preview release of Blazor WebAssembly! Please let us know what you think by filing issues on GitHub.

Thanks for trying out Blazor!
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: