BlazorComponentRegistry 0.0.1

dotnet add package BlazorComponentRegistry --version 0.0.1
                    
NuGet\Install-Package BlazorComponentRegistry -Version 0.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BlazorComponentRegistry" Version="0.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BlazorComponentRegistry" Version="0.0.1" />
                    
Directory.Packages.props
<PackageReference Include="BlazorComponentRegistry" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BlazorComponentRegistry --version 0.0.1
                    
#r "nuget: BlazorComponentRegistry, 0.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BlazorComponentRegistry@0.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BlazorComponentRegistry&version=0.0.1
                    
Install as a Cake Addin
#tool nuget:?package=BlazorComponentRegistry&version=0.0.1
                    
Install as a Cake Tool

Blazor Component Registry

Build

This library allows you to view which of your Blazor components are active on the page, as well as their current parameter values. The intended functionality is similar to React Dev Tools and Vue Dev Tools.

Installation

dotnet add package BlazorComponentRegistry

Add the following to your _Imports.razor file

@using BlazorComponentRegistry
@using BlazorComponentRegistry.Services
@using BlazorComponentRegistry.UI

Register the ComponentRegistryService in Program.cs

// Blazor Webassembly Program.cs
public static async Task Main(string[] args)
{
    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    builder.RootComponents.Add<App>("#app");
    builder.RootComponents.Add<HeadOutlet>("head::after");
    builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
    builder.Services.AddSingleton<IComponentRegistryService, ComponentRegistryService>();
    await builder.Build().RunAsync();
}
// Blazor Server Program.cs
public static async Task Main(string[] args)
{
    var builder = WebApplication.CreateBuilder(args);

    // Add services to the container.
    builder.Services.AddRazorPages();
    builder.Services.AddServerSideBlazor();
    builder.Services.AddScoped<IComponentRegistryService, ComponentRegistryService>();
    var app = builder.Build();

    ...

    app.Run();
}

Example Usage

Automatically Register and Unregister a component by inheriting from RegisterableComponent

@inherits RegisterableComponent

Manually Register and Unregister a component with the ComponentRegistryService

Your component should implement IDisposable so that the component instance becomes untracked when the component is disposed.

@implements IDisposable
...
@code {
    [Parameter] public string ParentComponentGuid { get; set; }
    private string componentGuid;

    protected override void OnInitialized()
    {
        componentGuid = componentRegistry.RegisterComponent(ParentComponentGuid, GetType()).ComponentGuid;
    }

    public void Dispose()
    {
        componentRegistry.UnregisterComponent(componentGuid);
    }
}

The RegisterComponent method will automatically generate a GUID string to uniquely identify the instance of the component

However, you can manually set its GUID if you wish. If a component with a duplicate GUID is registered, the RegisterComponent method will throw an exception.

@implements IDisposable
...
@code {
    [Parameter] public string ParentComponentGuid { get; set; }
    private string componentGuid = Guid.NewGuid().ToString();

    protected override void OnInitialized()
    {
        componentRegistry.RegisterComponent(ParentComponentGuid, GetType(), componentGuid);
    }

    public void Dispose()
    {
        componentRegistry.UnregisterComponent(componentGuid);
    }
}

To view the currently active, registered components, use the ComponentRegistry component.

@* I recommend putting it in your MainLayout if you want it to display on every page *@
<ComponentRegistry/>

Make sure you pass the ParentGuid from the parent to all registered child components.

Limitations

  • Each component you wish to monitor must either be registered manually in the component's code OR inherit from the RegisterableComponent base class provided by this library.
  • External Blazor Components, such as those provided by NuGet package, can't be tracked directly.
  • Recursive (self-referencing) Components are not supported.

Notes

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.1 339 1/8/2023