![]() |
|
Blazor WebAssembly 3.2.0 Preview 5 release now available - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: C#, Visual Basic & .NET (https://sickgaming.net/forum-79.html) +--- Thread: Blazor WebAssembly 3.2.0 Preview 5 release now available (/thread-95409.html) |
Blazor WebAssembly 3.2.0 Preview 5 release now available - xSicKxBot - 06-03-2020 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:
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 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.8If 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:
You’re all set! Read configuration during startup Configuration data is now available during app startup in Code: Program.MainCode: ConfigurationCode: WebAssemblyHostBuilderYou 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" }
}HTTP requests issued from a Blazor WebAssembly app using Code: HttpClientCode: fetchCode: HttpRequestMessageCode: fetchCode: Microsoft.AspNetCore.Components.WebAssembly.HttpCode: HttpRequestMessageFetch request property Code: SetBrowserRequestCredentialscredentials Code: SetBrowserRequestCachecache Code: SetBrowserRequestModemode Code: SetBrowserRequestIntegrityintegrity You can set additional options using the more generic Code: SetBrowserRequestOptionThe 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: SetBrowserResponseStreamingEnabledHonor 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: AuthorizationMessageHandlerCode: HttpClientCode: IAccessTokenProviderCode: AccessTokenNotAvailableExceptionCode: RedirectCode: AuthorizationMessageHandlerCode: ConfigureHandlerFor example, you can configure an Code: HttpClientCode: AuthorizationMessageHandlerCode: 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: BaseAddressAuthorizationMessageHandlerCode: HttpClientCode: BaseAddressAuthorizationMessageHandlerCode: 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: HttpClientCode: FetchDataCode: 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();
}Blazor now infers the user’s time zone and uses it in date and time calculations. In addition, APIs on Code: System.TimeZoneInfoHelp 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:
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! |