{"record":{"id":"ab89aa20190d18c1","repo":"dotnet/aspnetcore","slug":"a-reconnectpolicy-has-already-been-set","errorCode":null,"errorMessage":"A reconnectPolicy has already been set.","messagePattern":"A reconnectPolicy has already been set\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/ts/signalr/src/HubConnectionBuilder.ts","lineNumber":180,"sourceCode":"     * By default, the client will wait 0, 2, 10 and 30 seconds respectively before trying up to 4 reconnect attempts.\n     */\n    public withAutomaticReconnect(): HubConnectionBuilder;\n\n    /** Configures the {@link @microsoft/signalr.HubConnection} to automatically attempt to reconnect if the connection is lost.\n     *\n     * @param {number[]} retryDelays An array containing the delays in milliseconds before trying each reconnect attempt.\n     * The length of the array represents how many failed reconnect attempts it takes before the client will stop attempting to reconnect.\n     */\n    public withAutomaticReconnect(retryDelays: number[]): HubConnectionBuilder;\n\n    /** Configures the {@link @microsoft/signalr.HubConnection} to automatically attempt to reconnect if the connection is lost.\n     *\n     * @param {IRetryPolicy} reconnectPolicy An {@link @microsoft/signalR.IRetryPolicy} that controls the timing and number of reconnect attempts.\n     */\n    public withAutomaticReconnect(reconnectPolicy: IRetryPolicy): HubConnectionBuilder;\n    public withAutomaticReconnect(retryDelaysOrReconnectPolicy?: number[] | IRetryPolicy): HubConnectionBuilder {\n        if (this.reconnectPolicy) {\n            throw new Error(\"A reconnectPolicy has already been set.\");\n        }\n\n        if (!retryDelaysOrReconnectPolicy) {\n            this.reconnectPolicy = new DefaultReconnectPolicy();\n        } else if (Array.isArray(retryDelaysOrReconnectPolicy)) {\n            this.reconnectPolicy = new DefaultReconnectPolicy(retryDelaysOrReconnectPolicy);\n        } else {\n            this.reconnectPolicy = retryDelaysOrReconnectPolicy;\n        }\n\n        return this;\n    }\n\n    /** Configures {@link @microsoft/signalr.HubConnection.serverTimeoutInMilliseconds} for the {@link @microsoft/signalr.HubConnection}.\n     *\n     * @returns The {@link @microsoft/signalr.HubConnectionBuilder} instance, for chaining.\n     */\n    public withServerTimeout(milliseconds: number): HubConnectionBuilder {","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/ts/signalr/src/HubConnectionBuilder.ts#L162-L198","documentation":"Thrown by HubConnectionBuilder.withAutomaticReconnect when `this.reconnectPolicy` is already set. The builder enforces a single reconnect policy per connection; calling withAutomaticReconnect a second time on the same builder instance is rejected rather than silently overwriting the first.","triggerScenarios":"Invoking `.withAutomaticReconnect(...)` twice on the same HubConnectionBuilder — e.g. once with retry delays and once with a custom IRetryPolicy, or calling it in a shared builder factory that runs more than once.","commonSituations":"Composing builder configuration helpers that each call withAutomaticReconnect, or refactoring and forgetting to remove the earlier call.","solutions":["Call withAutomaticReconnect exactly once per builder.","If you need to merge, compute the final policy before calling and pass it in a single invocation.","Use a fresh HubConnectionBuilder for each connection rather than reusing/mutating one."],"exampleFix":"// before\nb.withAutomaticReconnect([0, 2000]).withAutomaticReconnect([0, 1000, 5000]);\n// after\nb.withAutomaticReconnect([0, 1000, 5000]);","handlingStrategy":"validation","validationCode":"function ensurePolicyOnce(b: { reconnectPolicy?: unknown }) {\n  if (b.reconnectPolicy) throw new Error(\"reconnectPolicy already configured\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call withAutomaticReconnect exactly once per builder.","Compute the final policy before configuring.","Use a fresh builder per connection."],"tags":["builder","reconnect","configuration"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}