Table of Contents

Services & Middleware

Extending services and middleware with the IPipeline interface

With .NET Core, Microsoft introduced new conventions on registering services for dependency injection and initializing middleware. These conventions are widely used today, and DynamicWeb 10 allows tapping directly in to these through the use of the IPipeline interface.

Note

Use the IPipeline interface to register services with the ASP.NET Core Services layer.

Use the IPipeline interface to add conventional ASP.NET Core Middleware to the DynamicWeb 10 application.

Background

For more info on how to use services (dependency injection) and middleware in ASP.NET Core please read the below references:

Getting Started

Extending services or adding middleware to a DynamicWeb application is very straightforward:

  • In your custom code project, reference the Dynamicweb.Host.Core NuGet package
  • Add a class inheriting from the IPipeline in your custom code repository
  • Implement the methods you need
  • Add or update the add-in in the DynamicWeb interface

Implementation Example

You can use the IPipeline interface by letting a class inheriting from it as below. If the project (dll or NuGet) is added as a DynamicWeb add-in, DynamicWeb 10 will load the class through reflection on application start-up.

using Dynamicweb.Host.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace CustomCode;

public class PipelineExtensions : IPipeline
{
    public int Rank => 101;

    public void RegisterApplicationComponents(IApplicationBuilder app)
    {
        app.UseMiddleware<SomeMiddleware>();
    }

    public void RegisterServices(IServiceCollection services, IMvcCoreBuilder mvcBuilder)
    {
        services.AddTransient<SomeService>();

        services.AddHttpClient<SomeHttpService>(
            options => options.BaseAddress = new Uri("https://www.dynamicweb.dk")
        );
    }

    public void RunInitializers()
    {
        var service = ServiceLocator.Current.GetInstance<SomeService>();
        service.Initialize();
    }
}

IPipeline.Rank

With middleware, the order in which the middleware is executed can be very important for your application to work as intended. To control this, you can use the Rank property. DynamicWeb 10 adds middleware from classes inheriting the IPipeline interface in ascending order, starting with the smallest Rank.

Tip

Be aware that DynamicWeb already uses the IPipeline interface for instantiating services and middleware. These implementations typically have a rank between 1 and 100.

To avoid interfering with the existing order, it is typically recommended to always use a rank larger than 100 (remember that middleware runs both before and after each request).

IPipeline.RegisterServices

Through the RegisterServices method, you can add services to the ASP.NET services layer through the IServiceCollection interface. For more info, please refer to Microsoft's official documentation: Dependency Injection in ASP.NET Core

A typically usecase could be for instantiating the HttpClient class without having to worry about disposal, socket exhaustion etc.

You can leave RegisterServices empty if you don't need services added.

IPipeline.RegisterApplicationComponents

Through the RegisterApplicationComponents method you can directly use the official IApplicationBuilder interface, which, for instance, allows you to add Middleware or Routing to the application. For more info, please refer to Microsoft's official documentation: ASP.NET Core Middleware

You can leave RegisterApplicationComponents empty if you don't need middleware added.

Caution

Editing the middleware pipeline can cause significant side-effects to the DynamicWeb 10 application and can cause the application to crash, malfunction (or not run at all).

Use middleware with care and always test extensively.

IPipeline.RunInitializers

There are cases, when you need specific initializations to run at application startup. For instance, DynamicWeb 10 applications often caches products on startup.

You can leave RunInitializers empty if you don't need it.

To top