{"record":{"id":"2d528844aa637dc7","repo":"microsoft/semantic-kernel","slug":"an-error-occurred-while-initializing-the-nameof-b","errorCode":null,"errorMessage":"An error occurred while initializing the {nameof(BedrockChatCompletionService)}: {ex.Message}","messagePattern":"An error occurred while initializing the (.+?): (.+?)","errorType":"exception","errorClass":"KernelException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Connectors/Connectors.Amazon/Bedrock/Extensions/BedrockServiceCollectionExtensions.cs","lineNumber":59,"sourceCode":"        }\n\n        service.AddKeyedSingleton<IChatCompletionService>(serviceId, (serviceProvider, _) =>\n        {\n            try\n            {\n                IAmazonBedrockRuntime runtime = bedrockRuntime ?? serviceProvider.GetRequiredService<IAmazonBedrockRuntime>();\n                var loggerFactory = serviceProvider.GetService<ILoggerFactory>();\n                // Check if the runtime instance is a proxy object\n                if (runtime.GetType().BaseType == typeof(AmazonServiceClient))\n                {\n                    // Cast to AmazonServiceClient and subscribe to the event\n                    ((AmazonServiceClient)runtime).BeforeRequestEvent += BedrockClientUtilities.BedrockServiceClientRequestHandler;\n                }\n                return new BedrockChatCompletionService(modelId, runtime, loggerFactory);\n            }\n            catch (Exception ex)\n            {\n                throw new KernelException($\"An error occurred while initializing the {nameof(BedrockChatCompletionService)}: {ex.Message}\", ex);\n            }\n        });\n\n        return service;\n    }\n\n    /// <summary>\n    /// Add Amazon Bedrock Text Generation service to the <see cref=\"IServiceCollection\" />.\n    /// </summary>\n    /// <param name=\"services\">The service collection.</param>\n    /// <param name=\"modelId\">The model for text generation.</param>\n    /// <param name=\"bedrockRuntime\">The optional <see cref=\"IAmazonBedrockRuntime\" /> to use. If not provided will be retrieved from the Service Collection.</param>\n    /// <param name=\"serviceId\">The optional service ID.</param>\n    /// <returns>Returns back <see cref=\"IServiceCollection\"/> with a configured service.</returns>\n    public static IServiceCollection AddBedrockTextGenerationService(\n        this IServiceCollection services,\n        string modelId,\n        IAmazonBedrockRuntime? bedrockRuntime = null,","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.Amazon/Bedrock/Extensions/BedrockServiceCollectionExtensions.cs#L41-L77","documentation":"Thrown by AddBedrockChatCompletion (the classic Kernel service DI extension, not the MEAI one) when the factory that constructs BedrockChatCompletionService throws. The factory resolves IAmazonBedrockRuntime from the provider, optionally casts to AmazonServiceClient to hook BeforeRequestEvent, then constructs the service. Any exception (most often a missing runtime registration) is wrapped in a KernelException.","triggerScenarios":"Calling services.AddBedrockChatCompletion(modelId, ...) without first registering an IAmazonBedrockRuntime in the container, so serviceProvider.GetRequiredService<IAmazonBedrockRuntime>() throws InvalidOperationException. Also when an explicit bedrockRuntime arg is a proxy/mock whose BaseType is not AmazonServiceClient (the cast is guarded, so this is benign) or the BedrockChatCompletionService ctor throws.","commonSituations":"Forgetting services.AddAWSService<IAmazonBedrockRuntime>() (or AddAmazonBedrockRuntime) before AddBedrockChatCompletion. Passing an invalid modelId. Container scoped-service issues in Blazor/async disposal.","solutions":["Register the Bedrock runtime first: services.AddAWSService<IAmazonBedrockRuntime>(); then services.AddBedrockChatCompletion(modelId);.","Pass an explicit IAmazonBedrockRuntime instance to AddBedrockChatCompletion if you construct it manually.","Inspect ex.InnerException for the real cause (typically 'No service for type IAmazonBedrockRuntime').","Validate the modelId is Converse-compatible."],"exampleFix":"// before\nservices.AddBedrockChatCompletion(\"anthropic.claude-3-5-sonnet-20240620-v1:0\");\n// KernelException: ... initializing the BedrockChatCompletionService: No service for type 'Amazon.BedrockRuntime.IAmazonBedrockRuntime'\n\n// after\nservices.AddAWSService<IAmazonBedrockRuntime>();\nservices.AddBedrockChatCompletion(\"anthropic.claude-3-5-sonnet-20240620-v1:0\");","handlingStrategy":"validation","validationCode":"// register the runtime before the chat completion service\nservices.AddAWSService<IAmazonBedrockRuntime>();\nservices.AddBedrockChatCompletion(modelId);","typeGuard":null,"tryCatchPattern":"try { services.AddBedrockChatCompletion(modelId); }\ncatch (KernelException ex) when (ex.InnerException?.Message.Contains(\"IAmazonBedrockRuntime\") == true)\n{ throw new InvalidOperationException(\"Register IAmazonBedrockRuntime (e.g. AddAWSService<IAmazonBedrockRuntime>()) first.\", ex); }","preventionTips":["Always register the Bedrock runtime before adding SK Bedrock services.","Pass an explicit IAmazonBedrockRuntime when you construct it manually.","Inspect KernelException.InnerException for the missing-service detail."],"tags":["bedrock","dependency-injection","startup","aws-sdk","csharp"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}