Services & Middleware
Extending services and middleware with the IPipeline interface
With .NET Core, Microsoft introduced new conventions for registering services for dependency injection and configuring middleware. Dynamicweb 10 fully supports these conventions using the IPipeline
interface.
Note
- Use
IPipeline
to register services with ASP.NET Core's dependency injection. - Use
IPipeline
to configure middleware in the Dynamicweb 10 application.
Background
For detailed information on services and middleware in ASP.NET Core, refer to these official Microsoft resources:
Getting Started
To extend Dynamicweb services or middleware:
- Reference the
Dynamicweb.Host.Core
NuGet package in your project. - Create a class implementing the
IPipeline
interface. - Implement necessary methods (
RegisterServices
,RegisterApplicationComponents
,RunInitializers
). - Register your extension as an add-in within the Dynamicweb administration.
Dynamicweb automatically loads your implementation at startup.
Implementation Example
Here's a basic example of using the IPipeline
:
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();
}
}
Implementation Example - Custom Web API
You can create custom web API endpoints in your Dynamicweb project by registering controllers and routing paths:
Step 1: Registering Controllers and Routing
using Dynamicweb.Host.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
public class CustomApiPipeline : IPipeline
{
public int Rank => 200;
public void RegisterApplicationComponents(IApplicationBuilder app)
{
app.MapWhen(IsCustomApiPath, apiApp =>
{
apiApp.UseRouting();
apiApp.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
});
static bool IsCustomApiPath(HttpContext context) =>
context.Request.Path.StartsWithSegments("/myapi", StringComparison.OrdinalIgnoreCase);
}
public void RegisterServices(IServiceCollection services, IMvcCoreBuilder mvcBuilder)
{
mvcBuilder.AddApplicationPart(typeof(MyApiController).Assembly);
}
public void RunInitializers()
{
// Optional initialization logic here
}
}
Step 2: Implementing the Controller
Create your API controller class:
using Microsoft.AspNetCore.Mvc;
namespace CustomCode.Controllers;
[ApiController]
[Route("myapi/test")]
public class MyApiController : ControllerBase
{
public class GreetingResponse
{
public string? Message { get; set; }
}
[HttpGet("hi")]
public IActionResult Hi([FromQuery] string name = "World")
{
return Ok(new GreetingResponse
{
Message = $"Hello {name}!"
});
}
}
This implementation registers the controller under /myapi/test
. Accessing /myapi/test/hi?name=Dynamicweb
returns {"Message": "Hello Dynamicweb!"}
.
Understanding IPipeline Concepts
IPipeline.Rank
Middleware execution order matters. Use the Rank
property to define execution sequence. Middleware is executed in ascending order by rank. Dynamicweb built-in middleware typically uses ranks 1–100. Set your rank above 100 to avoid conflicts.
IPipeline.RegisterServices
Register services needed by your application using IServiceCollection
. Common uses include configuring HttpClient
instances. Leave empty if no additional services are required.
IPipeline.RegisterApplicationComponents
Configure middleware or routing using the ASP.NET IApplicationBuilder
. Use carefully, as misconfigurations can severely impact your application. Leave empty if not required.
IPipeline.RunInitializers
Use this method to execute startup logic, such as populating caches or initializing stateful services. Leave empty if not needed.
Caution
Changes to middleware configuration can significantly affect application behavior. Always thoroughly test your configuration in a controlled environment.