Skip to content

Instantly share code, notes, and snippets.


Steve Sanderson SteveSandersonMS

  • Microsoft
View GitHub Profile
View EventUtil.cs
public static class EventUtil
// The repetition in here is because of the four combinations of handlers (sync/async * with/without arg)
public static Action AsNonRenderingEventHandler(Action callback)
=> new SyncReceiver(callback).Invoke;
public static Action<TValue> AsNonRenderingEventHandler<TValue>(Action<TValue> callback)
=> new SyncReceiver<TValue>(callback).Invoke;
public static Func<Task> AsNonRenderingEventHandler(Func<Task> callback)
=> new AsyncReceiver(callback).Invoke;
public static Func<TValue, Task> AsNonRenderingEventHandler<TValue>(Func<TValue, Task> callback)
View SectionContent.cs
using System;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Components
public class SectionContent : IComponent, IDisposable
private SectionRegistry _registry;
[Parameter] public string Name { get; set; }
SteveSandersonMS / Index.razor
Created Feb 20, 2020
Blazor WebAssembly use of ClientWebSocket
View Index.razor
@page "/"
@using System.Net.WebSockets
@using System.Text
@using System.Threading
@implements IDisposable
<h1>Echo test</h1>
<h3>State: @webSocket.State</h3>
@if (webSocket.State == WebSocketState.Open)
SteveSandersonMS / index.js
Created Dec 6, 2019
Trivial webpack setup
View index.js
console.log('Hello, world!');
SteveSandersonMS / Customer.cs
Created Sep 4, 2019
Blazor + FluentValidation example
View Customer.cs
public class Customer
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; } = new Address();
public List<PaymentMethod> PaymentMethods { get; } = new List<PaymentMethod>();
public class Address
View Index.razor
@page "/"
<h1>Hello, world!</h1>
Welcome to your new app.
<EditForm Model="@_editingPerson" OnValidSubmit="@OnValidSubmit" OnInvalidSubmit="@OnInvalidSubmit">
Name: <InputText @bind-Value="_editingPerson.Name" />
<ValidationMessage For="@(() => _editingPerson.Name)" />
SteveSandersonMS /
Last active Jan 10, 2021
Error handling in Server-Side Blazor

Error handling in Server-Side Blazor

Developers building Blazor applications should be aware of how the framework deals with exceptions, and what steps to take in order to maximize reliability and to detect and diagnose errors.

To recap, server-side Blazor is a stateful framework. For as long as users are interacting with your application, they maintain a connection to the server known as a circuit. The circuit holds all the active component instances, plus many other aspects of state such as the components' most recent render output and the current set of event-handling delegates that could be triggered by client-side events. If a user opens your application in multiple browser tabs, then they have multiple independent circuits.

As a high-level principle, Blazor treats most unhandled exceptions as fatal to that circuit. If a circuit is terminated due to an unhandled exception, the user can only continue by reloading the page to create a new circuit and starting again, although other circuits (e.g., th

SteveSandersonMS /
Last active Jan 21, 2021
Preserving State in Server-Side Blazor applications

Preserving State in Server-Side Blazor applications

Server-side Blazor is a stateful application framework. Most of the time, your users will maintain an ongoing connection to the server, and their state will be held in the server's memory in what's known as a "circuit". Examples of state held for a user's circuit include:

  • The UI being rendered (i.e., the hierarchy of component instances and their most recent render output)
  • The values of any fields and properties in component instances
  • Data held in DI service instances that are scoped to the circuit

Occasionally, users may experience a temporary network connection loss, after which Blazor will attempt to reconnect them to their original circuit so they can continue.

View RevalidatingAuthenticationStateProvider.cs
public class RevalidatingAuthenticationStateProvider : AuthenticationStateProvider, IDisposable
private static TimeSpan RefreshInterval = TimeSpan.FromMinutes(30);
private readonly CancellationTokenSource _cts;
private ClaimsPrincipal _currentUser;
public RevalidatingAuthenticationStateProvider(SignInManager<IdentityUser> signInManager)
_cts = new CancellationTokenSource();
SteveSandersonMS /
Created Jun 11, 2019
Blazor authentication and authorization

Authentication and Authorization

Authentication means determining who a particular user is. Authorization means applying rules about what they can do. Blazor contains features for handling both aspects of this.

It worth remembering how the overall goals differ between server-side Blazor and client-side Blazor:

  • Server-side Blazor applications run on the server. As such, correctly-implemented authorization checks are both how you determine which UI options to show (e.g., which menu entries are available to a certain user) and where you actually enforce access rules.
  • Client-side Blazor applications run on the client. As such, authorization is only used as a way of determining what UI options to show (e.g., which menu entries). The actual enforcement of authorization rules must be implemented on whatever backend server your application operates on, since any client-side checks can be modified or bypassed.

Authentication-enabled templates for Server-Side Blazor