{"record":{"id":"615df4fb04adc26a","repo":"MassTransit/MassTransit","slug":"no-bus-instances-found","errorCode":null,"errorMessage":"No bus instances found","messagePattern":"No bus instances found","errorType":"exception","errorClass":"ConfigurationException","httpStatus":null,"severity":"error","filePath":"src/MassTransit/Configuration/DependencyInjection/DependencyInjectionTestingExtensions.cs","lineNumber":244,"sourceCode":"        /// </summary>\n        [Obsolete(\"Use AddMassTransitTestHarness instead. Visit https://masstransit.io/obsolete for details.\")]\n        public static IServiceCollection AddMassTransitInMemoryTestHarness(this IServiceCollection services,\n            Action<IBusRegistrationConfigurator>? configure = null)\n        {\n            services.AddMassTransit(cfg =>\n            {\n                configure?.Invoke(cfg);\n\n                cfg.SetBusFactory(new InMemoryTestHarnessRegistrationBusFactory());\n            });\n            services.AddSingleton(provider =>\n            {\n                var busInstances = provider.GetService<IEnumerable<IBusInstance>>();\n                if (busInstances == null)\n                {\n                    var busInstance = provider.GetService<IBusInstance>();\n                    if (busInstance == null)\n                        throw new ConfigurationException(\"No bus instances found\");\n\n                    busInstances = [busInstance];\n                }\n\n                var testHarnessBusInstance = busInstances.FirstOrDefault(x => x is InMemoryTestHarnessBusInstance);\n                if (testHarnessBusInstance is InMemoryTestHarnessBusInstance testInstance)\n                    return testInstance.Harness;\n\n                throw new ConfigurationException(\"Test Harness configuration is invalid\");\n            });\n            services.AddSingleton<BusTestHarness>(provider => provider.GetRequiredService<InMemoryTestHarness>());\n\n            return services;\n        }\n\n        static (string? methodName, string? className) GetTestMethodInfo()\n        {\n            var stackTrace = new StackTrace(2);","sourceCodeStart":226,"sourceCodeEnd":262,"githubUrl":"https://github.com/MassTransit/MassTransit/blob/62ab339afa3bac2e9b3fe1769d0d35d7e44778e9/src/MassTransit/Configuration/DependencyInjection/DependencyInjectionTestingExtensions.cs#L226-L262","documentation":"Thrown by AddMassTransitInMemoryTestHarness when it resolves the test harness from the DI container and cannot find any IBusInstance in the provider (neither IEnumerable<IBusInstance> nor a single IBusInstance). The harness registration expects the bus to have already been configured and registered; without a bus instance there is no InMemoryTestHarnessBusInstance to serve the harness from. It is a ConfigurationException signalling the container was not configured as the testing extensions require.","triggerScenarios":"Calling provider.GetRequiredService<InMemoryTestHarness>() (or BusTestHarness) from a container where AddMassTransitInMemoryTestHarness ran but no bus was ever registered - e.g. the test harness extension was added without a corresponding bus registration, or the harness is resolved from a different/wrong IServiceProvider than the one the bus was configured in.","commonSituations":"Unit test projects where the harness registration is combined with a container setup that strips or replaces IBusInstance registrations; resolving the harness before the bus configuration completes; using a custom container builder that does not flow IBusInstance; mixing MassTransit versions where the testing package and core package diverge.","solutions":["Ensure AddMassTransitTestHarness (or AddMassTransitInMemoryTestHarness) is called on the same IServiceCollection that also configures a bus via AddMassTransit with AddBus / UsingInMemory, so IBusInstance registrations exist.","Build the provider and resolve InMemoryTestHarness after the bus configuration; do not resolve from a freshly created or empty ServiceProvider.","Verify MassTransit.TestFramework / MassTransit.TestFramework (testing) package versions match the core MassTransit package version.","Check that no code removes or replaces the IBusInstance registration in the container."],"exampleFix":"// before\nvar harness = new ServiceCollection()\n    .AddMassTransitInMemoryTestHarness()\n    .BuildServiceProvider();\nvar testHarness = harness.GetRequiredService<InMemoryTestHarness>(); // throws\n\n// after\nvar harness = new ServiceCollection()\n    .AddMassTransit(x =>\n    {\n        x.AddConsumer<OrderConsumer>();\n        x.UsingInMemory((context, cfg) => cfg.ConfigureEndpoints(context));\n        x.AddConfigureEndpointsCallback((name, cfg) => cfg.UseInMemoryOutbox(context));\n    })\n    .AddMassTransitInMemoryTestHarness()\n    .BuildServiceProvider();\nvar testHarness = harness.GetRequiredService<InMemoryTestHarness>();","handlingStrategy":"try-catch","validationCode":"var hasBusInstance = provider.GetService<IEnumerable<IBusInstance>>() != null || provider.GetService<IBusInstance>() != null;\nif (!hasBusInstance)\n    throw new InvalidOperationException(\"No IBusInstance registered; configure the bus before resolving the test harness.\");","typeGuard":"bool HasBusInstance(IServiceProvider p) =>\n    p.GetService<IEnumerable<IBusInstance>>() is { } list && list.Any()\n    || p.GetService<IBusInstance>() != null;","tryCatchPattern":"try { var harness = provider.GetRequiredService<InMemoryTestHarness>(); }\ncatch (ConfigurationException ex)\n{\n    // log: bus not configured; re-check AddMassTransit + harness registration\n}","preventionTips":["Always pair AddMassTransitInMemoryTestHarness with a full AddMassTransit(x => x.UsingInMemory(...)) registration on the same IServiceCollection.","Resolve the harness only from the built provider of that service collection, never a fresh container.","Keep MassTransit core and testing package versions in lockstep."],"tags":["masstransit","dependency-injection","test-harness","configuration"],"backgroundTag":"missing-dependency","analyzedSha":"62ab339afa3bac2e9b3fe1769d0d35d7e44778e9","analyzedAt":"2026-09-13T22:47:47.593Z","contentChangedAt":"2026-09-13T22:47:47.593Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}