{"record":{"id":"e26ae6145d950604","repo":"App-vNext/Polly","slug":"this-instance-of-circuitbreakerstateprovider-is","errorCode":null,"errorMessage":"This instance of 'CircuitBreakerStateProvider' is already initialized and cannot be used in a different circuit-breaker strategy.","messagePattern":"This instance of 'CircuitBreakerStateProvider' is already initialized and cannot be used in a different circuit-breaker strategy\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Polly.Core/CircuitBreaker/CircuitBreakerStateProvider.cs","lineNumber":14,"sourceCode":"namespace Polly.CircuitBreaker;\n\n/// <summary>\n/// Allows retrieval of the circuit breaker state.\n/// </summary>\npublic sealed class CircuitBreakerStateProvider\n{\n    private Func<CircuitState>? _circuitStateProvider;\n\n    internal void Initialize(Func<CircuitState> circuitStateProvider)\n    {\n        if (_circuitStateProvider != null)\n        {\n            throw new InvalidOperationException($\"This instance of '{nameof(CircuitBreakerStateProvider)}' is already initialized and cannot be used in a different circuit-breaker strategy.\");\n        }\n\n        _circuitStateProvider = circuitStateProvider;\n    }\n\n    /// <summary>\n    /// Gets a value indicating whether the state provider is initialized.\n    /// </summary>\n    /// <remarks>\n    /// The initialization happens when the circuit-breaker strategy is attached to this class.\n    /// This happens when the final strategy is created by the <see cref=\"ResiliencePipelineBuilder.Build\"/> call.\n    /// </remarks>\n    internal bool IsInitialized => _circuitStateProvider != null;\n\n    /// <summary>\n    /// Gets the state of the underlying circuit.\n    /// </summary>\n    public CircuitState CircuitState => _circuitStateProvider?.Invoke() ?? CircuitState.Closed;","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/App-vNext/Polly/blob/d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac/src/Polly.Core/CircuitBreaker/CircuitBreakerStateProvider.cs#L1-L32","documentation":"A CircuitBreakerStateProvider instance can be bound to exactly one circuit-breaker strategy. The binding happens lazily during ResiliencePipelineBuilder.Build() via the internal Initialize() method, which refuses to overwrite a non-null _circuitStateProvider. Passing the same provider instance into two different CircuitBreakerStrategyOptions causes the second Build() to throw this InvalidOperationException. It protects the provider from reporting the wrong circuit's state.","triggerScenarios":"Instantiating one CircuitBreakerStateProvider, assigning it to optionsA.StateProvider and optionsB.StateProvider (two separate CircuitBreakerStrategyOptions), then calling Build() on both builders. Also reusing a provider after a pipeline built with it has been constructed.","commonSituations":"Sharing a 'watcher' object across multiple named circuits in DI; refactoring a single circuit into two and forgetting to give each its own provider; copying options via memberwise clone that aliases the StateProvider reference.","solutions":["Create a new CircuitBreakerStateProvider instance for each circuit-breaker strategy instead of reusing one.","If you need one logical view over several circuits, keep a dictionary of providers (one per circuit name) rather than sharing a single instance.","Audit all CircuitBreakerStrategyOptions declarations to confirm no two reference the same StateProvider object reference."],"exampleFix":"// before\nvar provider = new CircuitBreakerStateProvider();\noptionsA.StateProvider = provider;\noptionsB.StateProvider = provider; // throws on second Build()\n\n// after\noptionsA.StateProvider = new CircuitBreakerStateProvider();\noptionsB.StateProvider = new CircuitBreakerStateProvider();","handlingStrategy":"validation","validationCode":"// Before Build(), ensure the provider is not already bound:\nif (provider.IsInitialized) {\n    throw new InvalidOperationException(\"Provider already bound to another circuit; create a new instance.\");\n}\n// Then assign and build. Track providers in a per-circuit dictionary.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never share one CircuitBreakerStateProvider across two CircuitBreakerStrategyOptions.","Keep a provider-to-circuit-name map so each circuit gets its own instance.","Avoid memberwise/record-with copies of options that alias the StateProvider reference."],"tags":["circuit-breaker","invalid-operation","state-provider","configuration","polly-core"],"backgroundTag":null,"analyzedSha":"d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac","analyzedAt":"2026-08-13T16:36:01.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}