{"record":{"id":"a48979b4f88598a1","repo":"elsa-workflows/elsa-core","slug":"build-must-be-called-before-accessing-services","errorCode":null,"errorMessage":"Build() must be called before accessing services","messagePattern":"Build\\(\\) must be called before accessing services","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/common/Elsa.Testing.Shared.Integration/WorkflowTestFixture.cs","lineNumber":45,"sourceCode":"    /// Initializes a new instance of the <see cref=\"WorkflowTestFixture\"/> class.\n    /// </summary>\n    /// <param name=\"testOutputHelper\">The test output helper</param>\n    public WorkflowTestFixture(ITestOutputHelper testOutputHelper)\n    {\n        _testApplicationBuilder = new(testOutputHelper);\n        CapturingTextWriter = new();\n        _testApplicationBuilder.WithCapturingTextWriter(CapturingTextWriter);\n    }\n\n    /// <summary>\n    /// Gets the capturing text writer that captures standard output from WriteLine activities.\n    /// </summary>\n    public CapturingTextWriter CapturingTextWriter { get; }\n\n    /// <summary>\n    /// Gets the service provider. Throws if Build() hasn't been called yet.\n    /// </summary>\n    public IServiceProvider Services => _services ?? throw new InvalidOperationException(\"Build() must be called before accessing services\");\n\n    /// <summary>\n    /// Configures Elsa features.\n    /// </summary>\n    /// <param name=\"configure\">Action to configure Elsa</param>\n    /// <returns>The fixture instance for method chaining</returns>\n    [UsedImplicitly]\n    public WorkflowTestFixture ConfigureElsa(Action<IModule> configure)\n    {\n        _testApplicationBuilder.ConfigureElsa(configure);\n        return this;\n    }\n\n    /// <summary>\n    /// Configures the service collection.\n    /// </summary>\n    /// <param name=\"configure\">Action to configure the service collection</param>\n    /// <returns>The fixture instance for method chaining</returns>","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/common/Elsa.Testing.Shared.Integration/WorkflowTestFixture.cs#L27-L63","documentation":"WorkflowTestFixture lazily builds an IServiceProvider from its configured Elsa features. Accessing the Services property before calling Build() throws this InvalidOperationException because the underlying service container does not exist yet. The fixture is designed for explicit build-then-use ordering so all configuration actions run before service resolution.","triggerScenarios":"Accessing the fixture's Services property (directly or via helpers that resolve services) before calling Build() on the fixture.","commonSituations":"Test setup code that resolves services in a constructor instead of in test initialization; copy-pasted test code where the Build() call was removed or reordered; fixtures subclassed with additional setup that runs before Build().","solutions":["Call Build() on the fixture before accessing Services.","Move service resolution into test setup that runs after Build().","Chain configuration calls then Build() in one place in the test initialize method."],"exampleFix":"// before\nvar services = fixture.Services;\n// after\nfixture.Build();\nvar services = fixture.Services;","handlingStrategy":"validation","validationCode":"if (fixture is not { } f) throw new InvalidOperationException(\"Fixture not initialized\");\nvar services = fixture.TestServiceState // or simply ensure Build() ran first:\nif (!built) fixture.Build();\nvar sp = fixture.Services;","typeGuard":"bool IsBuilt(WorkflowTestFixture f) => f.Services is IServiceProvider;","tryCatchPattern":"try { var sp = fixture.Services; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Build()\")) { fixture.Build(); var sp = fixture.Services; }","preventionTips":["Call Build() in the test initialize/constructor immediately after configuration.","Expose a helper property that builds lazily before returning services.","Never resolve services in base-class setup that runs before fixture Build()."],"tags":["dotnet","testing","dependency-injection","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"fe9217bdfa0e27f0e09e45006eb6898f616e513d","analyzedAt":"2026-09-13T20:32:34.702Z","contentChangedAt":"2026-09-13T20:32:34.702Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}