{"record":{"id":"06bef5f5b6acfc5d","repo":"ThreeMammals/Ocelot","slug":"unable-to-start-ocelot-error-injecting-middlewaretype","errorCode":null,"errorMessage":"Unable to start Ocelot: error injecting {middlewareType.FullName}, since its base type is not a(n) {typeof(TMiddlewareBase).FullName}!","messagePattern":"Unable to start Ocelot: error injecting (.+?), since its base type is not a\\(n\\) (.+?)!","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/Middleware/OcelotPipelineExtensions.cs","lineNumber":133,"sourceCode":"\r\n        return app.Build();\r\n    }\r\n\r\n    public static IApplicationBuilder UseIfNotNull(this IApplicationBuilder builder, Func<HttpContext, Func<Task>, Task> middleware)\r\n        => middleware != null ? builder.Use(middleware) : builder;\r\n\r\n    public static IApplicationBuilder UseIfNotNull<TMiddleware>(this IApplicationBuilder builder, Func<HttpContext, Func<Task>, Task> middleware, bool addDefault = true)\r\n        where TMiddleware : OcelotMiddleware\r\n        => middleware != null ? builder.Use(middleware)\r\n            : addDefault ? builder.UseMiddleware<TMiddleware>()\r\n            : builder;\r\n\r\n    public static IApplicationBuilder UseIfNotNull<TMiddlewareBase>(this IApplicationBuilder builder, Type middlewareType)\r\n        where TMiddlewareBase : OcelotMiddleware\r\n    {\r\n        if (middlewareType is null) return builder;\r\n        if (!middlewareType.BaseType.Equals(typeof(TMiddlewareBase)))\r\n            throw new Exception($\"Unable to start Ocelot: error injecting {middlewareType.FullName}, since its base type is not a(n) {typeof(TMiddlewareBase).FullName}!\");\r\n        return builder.UseMiddleware(middlewareType);\r\n    }\r\n\r\n    public static void ConfigureWebSockets(this IApplicationBuilder app, OcelotPipelineConfiguration configuration)\r\n    {\r\n        app.UseMiddleware<DownstreamRouteFinderMiddleware>();\r\n        app.UseMiddleware<MultiplexingMiddleware>();\r\n        app.UseMiddleware<SecurityMiddleware>(); // TODO Consider moving this before MultiplexingMiddleware\r\n        app.UseMiddleware<DownstreamRequestInitialiserMiddleware>();\r\n        app.UseMiddleware<LoadBalancingMiddleware>();\r\n        app.UseMiddleware<DownstreamUrlCreatorMiddleware>();\r\n        app.UseIfNotNull<WebSocketsProxyMiddleware>(configuration.WebSocketsMiddlewareType);\r\n        app.UseIfNotNull<WebSocketsProxyMiddleware>(configuration.WebSocketsMiddleware, configuration.WebSocketsMiddlewareType is null);\r\n    }\r\n}\r\n","sourceCodeStart":115,"sourceCodeEnd":149,"githubUrl":"https://github.com/ThreeMammals/Ocelot/blob/d1f22d930430410bfd0233f5e9e4f92980cf7df0/src/Middleware/OcelotPipelineExtensions.cs#L115-L149","documentation":"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.","triggerScenarios":"Thrown at src/Middleware/OcelotPipelineExtensions.cs:133 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","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."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"d1f22d930430410bfd0233f5e9e4f92980cf7df0","analyzedAt":"2026-09-12T13:50:06.067Z","contentChangedAt":"2026-09-12T13:50:06.067Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}