ThreeMammals/Ocelot · error · Exception
Unable to start Ocelot: error injecting
Error message
Unable to start Ocelot: error injecting {middlewareType.FullName}, since its base type is not a(n) {typeof(TMiddlewareBase).FullName}! What it means
This is a pipeline-construction guard in OcelotPipelineExtensions.UseIfNotNull<TMiddleware>. When a named Ocelot middleware delegate is absent from the custom pipeline configuration, the method falls back to builder.UseMiddleware<TMiddleware>() to register the default middleware; before doing so it requires TMiddleware to derive from the expected base type (OcelotMiddleware or the middleware-chain base parameterized as TMiddlewareBase). If the generic type supplied by the pipeline extension methods is not assignable to that base, the DI container could not inject the middleware's dependencies (RequestDelegate, IOcelotLoggerFactory, etc.), so the method throws this error immediately while building the app pipeline — a configuration/programming error at Ocelot wiring time, before any HTTP request is served. The input at fault is the TMiddleware type argument.
Solutions
- Make the middleware type passed to UseIfNotNull derive from OcelotMiddleware (or the expected TMiddlewareBase) so constructor injection of RequestDelegate/IOcelotLoggerFactory works.
- Register the middleware's dependencies (logger factory, route repository, etc.) in IServiceCollection before UseOcelot builds the pipeline.
- Check that you are calling the correct UseIfNotNull overload matching your middleware's base class.
- If adding a truly custom middleware that is not an OcelotMiddleware, use plain builder.Use(...) with a Func<HttpContext, Func<Task>, Task> instead of the generic fallback.
- Verify no stale Ocelot assembly/version mismatch causes the base type identity check to fail.
Defensive patterns
Strategy: type-guard
When it happens
Trigger: Thrown at src/Middleware/OcelotPipelineExtensions.cs:133 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of ThreeMammals/Ocelot@d1f22d9304 (2026-09-12).
Data as JSON: /api/errors/06bef5f5b6acfc5d.
Report an issue: GitHub.
Appendix: source
Thrown at src/Middleware/OcelotPipelineExtensions.cs:133
return app.Build();
}
public static IApplicationBuilder UseIfNotNull(this IApplicationBuilder builder, Func<HttpContext, Func<Task>, Task> middleware)
=> middleware != null ? builder.Use(middleware) : builder;
public static IApplicationBuilder UseIfNotNull<TMiddleware>(this IApplicationBuilder builder, Func<HttpContext, Func<Task>, Task> middleware, bool addDefault = true)
where TMiddleware : OcelotMiddleware
=> middleware != null ? builder.Use(middleware)
: addDefault ? builder.UseMiddleware<TMiddleware>()
: builder;
public static IApplicationBuilder UseIfNotNull<TMiddlewareBase>(this IApplicationBuilder builder, Type middlewareType)
where TMiddlewareBase : OcelotMiddleware
{
if (middlewareType is null) return builder;
if (!middlewareType.BaseType.Equals(typeof(TMiddlewareBase)))
throw new Exception($"Unable to start Ocelot: error injecting {middlewareType.FullName}, since its base type is not a(n) {typeof(TMiddlewareBase).FullName}!");
return builder.UseMiddleware(middlewareType);
}
public static void ConfigureWebSockets(this IApplicationBuilder app, OcelotPipelineConfiguration configuration)
{
app.UseMiddleware<DownstreamRouteFinderMiddleware>();
app.UseMiddleware<MultiplexingMiddleware>();
app.UseMiddleware<SecurityMiddleware>(); // TODO Consider moving this before MultiplexingMiddleware
app.UseMiddleware<DownstreamRequestInitialiserMiddleware>();
app.UseMiddleware<LoadBalancingMiddleware>();
app.UseMiddleware<DownstreamUrlCreatorMiddleware>();
app.UseIfNotNull<WebSocketsProxyMiddleware>(configuration.WebSocketsMiddlewareType);
app.UseIfNotNull<WebSocketsProxyMiddleware>(configuration.WebSocketsMiddleware, configuration.WebSocketsMiddlewareType is null);
}
}
View on GitHub (pinned to d1f22d9304)