Plugin Architecture in Blazor – A How To Guide

In modern software development, modularity is a key principle. It promotes the creation of software components that are independent and organized. This approach aids in efficient development, maintenance, and scalability and the plugin architecture aligns with this principle. This article will be a step-by-step guide to using Autofac to create an application with a plugin architecture in Blazor.

Within C# web development, frameworks like Blazor have highlighted the utility of plugins. Blazor’s component-based structure is conducive to the implementation of plugins, offering developers a method to expand applications without extensive modifications. So if that sounds exciting, let’s get into it!

Understanding Plugin Architecture

A plugin architecture is a design pattern that enables the addition of new functionalities to a system without changing its existing structure. It’s akin to adding modules to a base system; the primary system remains intact, but its functions are expanded.

There are several reasons to consider a plugin-based approach for web applications:

  1. Flexibility: Plugins provide a way to introduce or alter features without modifying the core application. This adaptability ensures the application can meet new requirements without significant changes.
  2. Maintainability: Plugins are distinct from the main application, allowing for independent updates or fixes. This separation means that alterations to one plugin don’t impact others or the central application.
  3. Scalability: As the application’s needs grow, it can incorporate new plugins. This modular approach ensures manageability, even as the application becomes more complex.
  4. Collaboration: Different development teams or individuals can work on separate plugins at the same time, facilitating parallel development.

The following sections will detail how to implement this architecture in a Blazor application, using Autofac for dependency management.

Setting the Stage: Tools and Technologies

Introduction to Blazor

Blazor is a framework from Microsoft that allows developers to build interactive web applications using C# instead of JavaScript. It’s part of the .NET ecosystem and offers two hosting models:

  1. Blazor WebAssembly: This model runs the client-side C# code directly in the browser using WebAssembly. It allows for the development of single-page applications (SPAs) with .NET.
  2. Blazor Server: In this model, the C# code runs on the server, and the UI updates, event handling, and JavaScript calls are handled over a real-time SignalR connection.

Blazor’s component-based architecture makes it a suitable candidate for implementing a plugin system. Each component in Blazor is a self-contained unit of code, UI, and any associated logic, making it modular by design.

Autofac in the Context of Blazor

Autofac is a popular inversion of control (IoC) container for .NET. It provides dependency injection (DI) capabilities, which is a technique to achieve Inversion of Control between classes and their dependencies.

In the context of Blazor, Autofac can be used to manage the dependencies of Blazor components. Dependency injection is crucial in modern web development for several reasons:

  1. Separation of Concerns: DI promotes a separation between the creation of objects and the business logic that uses them. This separation makes the codebase cleaner and more maintainable.
  2. Testability: By injecting dependencies, it becomes easier to replace real implementations with mock objects, facilitating unit testing.
  3. Flexibility: DI allows developers to switch out implementations without changing the components that depend on them. This flexibility is especially beneficial in a plugin architecture where different plugins might have varying implementations for shared interfaces.

Next, let’s delve into how these can be combined to create a robust plugin architecture in Blazor with Autofac.

Designing a Plugin Architecture in Blazor

Core Components for a Plugin Architecture in Blazor

When designing a plugin system for Blazor using Autofac, it’s essential to understand the core components that make up this architecture. These components ensure modularity and extensibility, allowing developers to add or remove features with ease.

  • Host Application: The host application is the primary Blazor application that serves as the foundation for the plugins. It provides the necessary infrastructure to load, manage, and interact with plugins. The host application should be designed with extensibility in mind, ensuring that it can accommodate new plugins without significant changes. This involves setting up the necessary routes, layouts, and dependency injection configurations to support the dynamic addition of plugins.
  • Plugin Interface: The plugin interface is a crucial component in the plugin architecture. It defines a contract that all plugins must adhere to. This contract ensures a consistent way for the host application to communicate with and manage plugins. By defining methods, properties, or events in the interface, you set clear expectations for what a plugin should provide. For instance, the interface might specify methods for initializing the plugin, rendering its UI, or handling specific events.

    In the context of Autofac, the plugin interface also plays a vital role in dependency injection. By registering the interface with Autofac, you can ensure that the correct plugin implementations are provided wherever the interface is required.
  • Plugins: Plugins are individual modules that extend the functionality of the host Blazor application. Each plugin is a self-contained unit, encapsulating specific features or functionalities. When developing plugins, it’s essential to implement the previously defined plugin interface. This ensures that the host application can seamlessly integrate the plugin.

    In a Blazor application, a plugin might consist of multiple components, services, or even additional dependencies. Using Autofac, these dependencies can be registered and resolved within the plugin, ensuring that each plugin can operate independently while still integrating smoothly with the host application.

Integrating Autofac into a Blazor Project

YouTube player

To start using Autofac in a Blazor application, you’ll first need to add the necessary NuGet packages. For a Blazor WebAssembly project, you’d typically require Autofac.Extensions.DependencyInjection and Autofac.

Once the packages are added, you can configure Autofac in the Program.cs file of your Blazor application. Instead of using the default DI container provided by Blazor, you’ll replace it with Autofac.

Configure With WebAssemblyHostBuilder

If you’re using WebAssemblyHostBuilder, here’s a basic setup:

public static async Task Main(string[] args)
    var builder = WebAssemblyHostBuilder.CreateDefault(args);

    // Set up Autofac
    var containerBuilder = new ContainerBuilder();
    containerBuilder.RegisterModule(new YourAutofacModule());
    // ... any other Autofac registrations ...

    var host = builder.Build();

    await host.RunAsync();

Configure With WebApplicationBuilder

If you’re using a WebApplicationBuilder, things need to work a little bit differently! Here’s a basic setup:

var builder = WebApplication.CreateBuilder(args);
var containerBuilder = new ContainerBuilder();

// Add services to the container.

builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(containerBuilder =>
    containerBuilder.RegisterModule(new YourAutofacModule());
    // ... any other Autofac registrations ...

var app = builder.Build();

Configuring Dependency Injection with Autofac

With Autofac integrated, you can now leverage its powerful features for dependency injection. For instance, if you have services or repositories that your Blazor components depend on, you can register them with Autofac and let it handle their lifecycle.

When it comes to plugins, Autofac becomes even more valuable. You can register the plugin interface and its implementations, ensuring that the correct plugin is provided wherever the interface is required. Additionally, if your plugins have their own dependencies, Autofac can manage those as well, ensuring that each plugin gets the services it needs.

In the context of a plugin architecture, you might also find Autofac’s named and keyed service registrations useful. These allow you to register multiple implementations of an interface and then resolve a specific one based on a name or key. This can be particularly handy when dealing with multiple plugins that implement the same interface.

In the upcoming sections, we’ll explore how to define and implement the plugin interface, and how Autofac can be used to manage the plugins and their dependencies in a Blazor application.

An Example Plugin Architecture In Blazor

In this section, we’ll walk through the process of building a rudimentary system for a plugin architecture in Blazor. This will involve defining a clear interface for plugins and then implementing the system within Blazor, leveraging Autofac for dependency resolution. While basic in nature, the concepts can be built upon.

Defining the Plugin Interface

At the heart of any plugin system is a well-defined interface. This interface sets the contract that all plugins must adhere to, ensuring consistency and interoperability.

Here’s a basic example of what a plugin interface might look like:

public interface IPlugin
    string Name { get; }
    string Description { get; }
    void Execute();

In this simple example, each plugin must have a name, a description, and an Execute method that carries out the plugin’s primary function.

Implementing the Plugin Architecture in Blazor

With the interface in place, we can now focus on how to manage and utilize plugins within our Blazor application. We’ll start with loading plugins.

One common approach for loading plugins is to have a designated folder in your application where all plugins are stored. During the application’s startup, you can scan this directory, load each plugin, and register it with Autofac.

var pluginType = typeof(IPlugin);
var plugins = Directory.GetFiles("path_to_plugins_folder", "*.dll")
    .SelectMany(assemblyPath => Assembly.LoadFrom(assemblyPath).GetTypes())
    .Where(p => pluginType.IsAssignableFrom(p) && !p.IsInterface)

foreach (var plugin in plugins)

Once the plugins are loaded and registered, you can use Autofac to resolve them wherever needed in your application. This ensures that each plugin gets the dependencies it requires.

For instance, if you have a component that needs to list all available plugins:

@inject IEnumerable<IPlugin> Plugins

@foreach (var plugin in Plugins)
        <button @onclick="plugin.Execute">Run Plugin</button>

In this example, Autofac automatically provides all registered implementations of IPlugin to the Plugins property, and the component then displays them.

By following these steps, you can establish a basic system with a plugin architecture in Blazor, with Autofac handling the heavy lifting of dependency resolution. As you expand and refine your system, you’ll find that this combination of tools offers a robust and flexible foundation for modular development.

What’s Next For Creating a Plugin Architecture in Blazor?

In this article, we looked at the benefits of using a plugin architecture and how they can be useful for web applications. To create a plugin architecture in Blazor, we saw that we could use Autofac for our dependency injection and inversion of control. While not the only option for this, Autofac makes it a breeze.

A plugin architecture in Blazor can be extended to more advanced scenarios than we looked at here. In follow-up articles, we’ll explore these! If you don’t want to miss out, consider subscribing to the weekly newsletter to be informed about the latest content for the week!


These are products & services that I trust, use, and love. I get a kickback if you decide to use my links. There’s no pressure, but I only promote things that I like to use!

      • RackNerd: Cheap VPS hosting options that I love for low-resource usage!
      • Contabo: Alternative VPS hosting options with very affordable prices!
      • ConvertKit: This is the platform that I use for my newsletter!
      • SparkLoop: This service helps me add different value to my newsletter!
      • Opus Clip: This is what I use for help creating my short-form videos!
      • Newegg: For all sorts of computer components!
      • Bulk Supplements: For an enormous selection of health supplements!
      • Quora: I try to answer questions on Quora when folks request them of me!

    author avatar
    Nick Cosentino Principal Software Engineering Manager
    Principal Software Engineering Manager at Microsoft. Views are my own.

    This Post Has One Comment

    1. Kevin Moens

      We have requirements where we build ERP software. This software typically has custom modifications on 70%+ of our code base. We have 1,000s of customers with modifications on a matrix of versions. The manufacturing world doesn’t upgrade regularly. Since each company has their own way of being competitive in their space, most of the time have modifications. With this we are looking to upgrade from our WPF applications that are desktop bound. We have two types of applications. We have an office application for integrations to documents. We support having smart cards for security, scanners to load in documents, drag and drop on desktop applications from the File System where we hold file paths to their network files. We also allow documents to be uploaded and filed away to cloud storage locations (SharePoint, S3, etc). Then we have a shop floor application (also WPF) that we use biometric devices to login, we print labels, among some of the same things that the documents application does. We want to support Mac, iOS and Android. Yet since almost every customer is modified or is on an old version and would need to distribute these apps to many clients, I have been working on designing a prototype. The concept is that we build a MAUI app that hosts a BlazorWebView. This app will have a basic configuration and login screen. These apps then can be hosted in the general App Stores. Then when a customer downloads the application, they can put in URLs to their live, test, demo, etc… servers. Those URLs then pull down the BlazorShared libraries for the applications the user can run. Then they can run the BlazorShared applications that match the version of the ERP they are on. Many customers will not update in a timely manner, yet the MAUI app can still get updates to have new functionality to talk to the native device from the app stores. If they need a modification or a fix, they can load the software on their server and have their clients either have an update pushed or pulled from the client.

      Now for the question. Would you recommend any suggestions for resources? I am thinking that this article would give us a ground level basis on the plugin design.
      Our team hasn’t fully committed to Maui with a Blazor UI. Yet our team also would want a web front end for some of the actions. It wouldn’t be as fully featured, but would still be usable for many clients. Multiple on our team prefer Xaml and even looked at Uno and Avalonia. I am leaning toward the future and talent pool in the area. Finding a Xaml developer is very tough. Do you have an opinion on the route you would lead your team in my situation? Stick to learning Blazor or stick to Xaml?
      Thanks from continued follower. Thanks for the diverse content.

    Leave a Reply