{"record":{"id":"6d5d4f6a75cff07c","repo":"dotnet/aspnetcore","slug":"the-render-handle-is-not-yet-assigned","errorCode":null,"errorMessage":"The render handle is not yet assigned.","messagePattern":"The render handle is not yet assigned\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/RenderHandle.cs","lineNumber":123,"sourceCode":"        _renderer.AddToRenderQueue(_componentId, renderFragment);\n    }\n\n    /// <summary>\n    /// Dispatches an <see cref=\"Exception\"/> to the <see cref=\"Renderer\"/>.\n    /// </summary>\n    /// <param name=\"exception\">The <see cref=\"Exception\"/> that will be dispatched to the renderer.</param>\n    /// <returns>A <see cref=\"Task\"/> that will be completed when the exception has finished dispatching.</returns>\n    public Task DispatchExceptionAsync(Exception exception)\n    {\n        var renderer = _renderer;\n        var componentId = _componentId;\n        return Dispatcher.InvokeAsync(() => renderer!.HandleComponentException(exception, componentId));\n    }\n\n    [DoesNotReturn]\n    private static void ThrowNotInitialized()\n    {\n        throw new InvalidOperationException(\"The render handle is not yet assigned.\");\n    }\n}\n","sourceCodeStart":105,"sourceCodeEnd":126,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/Components/src/RenderHandle.cs#L105-L126","documentation":"RenderHandle.ThrowNotInitialized is a [DoesNotReturn] helper called by the Dispatcher property getter and the Render method when _renderer is null. It throws InvalidOperationException with the message 'The render handle is not yet assigned.' This is the most commonly hit variant because Dispatcher is used extensively (e.g., invoking back to the sync context) and Render is called by ComponentBase.StateHasChanged().","triggerScenarios":"Calling StateHasChanged() or accessing RenderHandle.Dispatcher on a component whose RenderHandle has not been assigned. This happens in tests that call lifecycle methods directly without a renderer, or when a component calls StateHasChanged during construction (before the handle is set). It can also occur if code captures a RenderHandle from a disposed or never-initialized component and later calls Render on it.","commonSituations":"Unit tests invoking SetParametersAsync or StateHasChanged on a manually-constructed component; calling StateHasChanged inside a constructor; components used outside the Blazor pipeline (e.g., in a plain C# service); incorrect bUnit setup.","solutions":["Use TestRenderer or bUnit's TestContext to properly attach the render handle before calling StateHasChanged or accessing Dispatcher.","Ensure StateHasChanged / Render is only called after the component is initialized (OnInitialized or later), never in the constructor.","Guard with RenderHandle.IsInitialized before calling StateHasChanged in code paths that may run pre-initialization.","Ensure the component is rendered through the normal rendering pipeline rather than invoked manually."],"exampleFix":"// before (test)\nvar component = new MyComponent();\ncomponent.StateHasChanged(); // calls Render on uninitialized handle -> throws\n\n// after (bUnit)\nusing var ctx = new TestContext();\nvar cut = ctx.RenderComponent<MyComponent>();\n// StateHasChanged works inside component lifecycle","handlingStrategy":"type-guard","validationCode":"if (renderHandle.IsInitialized)\n{\n    renderHandle.Render(builder => { /* ... */ });\n}\n// Or guard StateHasChanged-equivalent logic\nif (renderHandle.IsInitialized)\n{\n    await renderHandle.Dispatcher.InvokeAsync(() => { /* ... */ });\n}","typeGuard":"static bool CanRender(RenderHandle handle) => handle.IsInitialized;\n\n// In ComponentBase overrides, guard StateHasChanged calls:\nprotected void SafeStateHasChanged()\n{\n    if (renderHandle.IsInitialized) StateHasChanged();\n}","tryCatchPattern":null,"preventionTips":["Never call StateHasChanged from a component constructor.","Always use a test renderer (bUnit/TestRenderer) in unit tests.","Check RenderHandle.IsInitialized before calling Render or accessing Dispatcher.","Avoid capturing RenderHandle for deferred use unless you verify initialization."],"tags":["blazor","renderhandle","dispatcher","statehaschanged","lifecycle","testing"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}