Posts

CancellationToken in Blazor: Stop Doing Work After the User Leaves

Image
A user opens a page in your Blazor app, and a database query begins. Before the query finishes, they click away to another menu item. In many Blazor applications, that original database query just keeps running. The component is no longer visible, the result is no longer needed, and the user is three pages away—yet your server is still allocating memory, executing SQL, consuming connection pool slots, and awaiting HTTP requests. When the operation finally completes, it delivers results to a component instance that has already been disposed. This is precisely what CancellationToken is designed to solve. However, implementing cancellation in Blazor requires a clear distinction between disposable UI work and durable business operations . The Problem in Plain Code Consider a standard Blazor component reading from an EF Core service: Razor CSHTML @page "/customers" @inject CustomerService CustomerService <h1>Customers</h1> @if (customers is null) { <p>Loa...

Your Blazor State Might Belong in the URL

Image
  When building a Blazor component, storing UI state directly inside class fields is the default instinct: C# private string ? searchText; private string ? status; private string sortColumn = "Name" ; private int page = 1 ; There’s nothing inherently broken about this. But think about the user experience. A user spends three minutes filtering down a customer list: they search for "Microsoft" , filter by Active , sort by LastContact , and jump to page 3 . Their screen shows a precise slice of data. Yet, their browser address bar still stubbornly reads: /customers If they refresh, their work is wiped out. If they bookmark the page, they lose the context. If they drop that link into Teams or Slack for a colleague, the recipient lands on page 1 of an unfiltered list. Compare that to: /customers?search=Microsoft&status=Active&sort=LastContact&page=3 By shifting page-level view parameters into query strings, the URL becomes a deterministic blueprint of ...