Skip to content

Instantly share code, notes, and snippets.

Steve Sanderson SteveSandersonMS

  • Microsoft
View GitHub Profile
SteveSandersonMS /
Last active Apr 24, 2019
Why sequence numbers should relate to code line numbers, not execution order

Why sequence numbers should relate to code line numbers, not execution order

Or in other words, why you should hard-code sequence numbers, and not generate them programmatically.

Unlike .jsx files, .razor/.cshtml files are always compiled. This is potentially a great advantage for .razor, because we can use the compile step to inject information that makes things better or faster at runtime.

A key example of this are sequence numbers. These indicate to the runtime which outputs came from which distinct and ordered lines of code. The runtime uses this information to generate efficient tree diffs in linear time, which is far faster than is normally possible for a general tree diff algorithm.


SteveSandersonMS / ITab.cs
Created Nov 15, 2018
Blazor tab example
View ITab.cs
using Microsoft.AspNetCore.Blazor;
public interface ITab
RenderFragment ChildContent { get; }
View Cross-component

Cross-component interactions

Blazor should make it easy to build all the classic high-level UI widgets, such as forms, grids, tabs, dialogs, and so on. One of the big technical pieces needed for this is templated components, and since 0.6.0 we have strong support for that. Another necessary bit of infrastructure is a mechanism for components to collaborate based on ancestor/descendant relationships. For example:

  • In a Form component, you'd want nested Input components to be able to affect the form's validation on submit.
  • In a TabSet component, you'd want the TabSet to discover nested Tab components so it knows what tab titles to display, and to render only the active one (either by TabSet choosing which child to render, or by Tab knowing whether it's currently the active one).
  • In a NavBar component, you'd want a HamburgerButton component to be able to send a "toggle expansion" signal to the NavBar.

As of today, all these are possible but only through manual wiring. For exa

SteveSandersonMS / BlazorForm.cshtml
Created Sep 21, 2018
Validation mockup A: explicit <ValidateXyz> components that take a Func<T>
View BlazorForm.cshtml
@* Unfortunately this has to be named BlazorForm, not Form, because we can't differentiate based on casing *@
<form onsubmit=@HandleSubmit>
@functions {
private FormContext _context = new FormContext();
[Parameter] protected RenderFragment<FormContext> ChildContent { get; set; }
[Parameter] protected Action<FormContext> OnSubmit { get; set; }
View linker

Blazor: Adding a custom linker config

In your csproj file, add the following:

  <Target Name="AddCustomLinkerDescriptor" AfterTargets="_CollectBlazorLinkerDescriptors">
      <BlazorLinkerDescriptor Include="CustomLinkerConfig.xml" />
View TaskResultUtil.cs
class TaskResultUtil
private static ConcurrentDictionary<Type, ITaskResultGetter> _cachedGetters = new ConcurrentDictionary<Type, ITaskResultGetter>();
private interface ITaskResultGetter
object GetResult(Task task);
private class TaskResultGetter<T> : ITaskResultGetter
View Program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
SteveSandersonMS / Home.cshtml
Created Feb 20, 2018
Manually implemented HTTP client
View Home.cshtml
@using StandaloneApp.Util
<h1>Hello, world!</h1>
Welcome to your new app.
<button @onclick(DoRequest)>Do request</button>
<div><strong>Response: </strong>@responseText</div>
@functions {
SteveSandersonMS / somefile.cs
Last active May 9, 2016 — forked from SteveSanderson/somefile.cs
Async void message loop
View somefile.cs
public class MyNetworkClient {
public event SomeDelegate OnException;
public async Task ConnectAsync(string address) {
await this.MakeTheActualConnection(address);
// It's async void! But is that bad?
// I know that an unhandled exception here is going to bring down the process, but where else do you want
You can’t perform that action at this time.