{"record":{"id":"42c6d2759f41424d","repo":"microsoft/aspire","slug":"the-application-has-not-been-initialized","errorCode":null,"errorMessage":"The application has not been initialized.","messagePattern":"The application has not been initialized\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Testing/DistributedApplicationFactory.cs","lineNumber":454,"sourceCode":"\n    private void OnEntryPointExit(Exception? exception)\n    {\n        if (exception is not null)\n        {\n            _exitTcs.TrySetException(exception);\n            OnException(exception);\n        }\n        else\n        {\n            _exitTcs.TrySetResult();\n        }\n    }\n\n    private void ThrowIfNotInitialized()\n    {\n        if (!_startedTcs.Task.IsCompletedSuccessfully)\n        {\n            throw new InvalidOperationException(\"The application has not been initialized.\");\n        }\n    }\n\n    private DistributedApplication GetStartedApplication()\n    {\n        ThrowIfNotInitialized();\n        return _appTcs.Task.GetAwaiter().GetResult();\n    }\n\n    private void OnException(Exception exception)\n    {\n        _appTcs.TrySetException(exception);\n        _builderTcs.TrySetException(exception);\n        _startedTcs.TrySetException(exception);\n    }\n\n    private void OnDisposed()\n    {","sourceCodeStart":436,"sourceCodeEnd":472,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Testing/DistributedApplicationFactory.cs#L436-L472","documentation":"Thrown by ThrowIfNotInitialized when GetStartedApplication (or members relying on it) is called before the application factory has finished starting successfully. The factory tracks startup completion via _startedTcs; accessing the application too early or after a failed start raises this error.","triggerScenarios":"Calling factory.GetStartedApplication() (or accessing HttpClient/Resources that depend on it) before awaiting StartAsync/CreateAsync completion, or after startup failed so _startedTcs never completed successfully.","commonSituations":"Not awaiting the start task before asserting on resources; using the factory in a constructor or field initializer where the async start hasn't finished; startup crashed earlier (e.g. a container failed) so the app never initialized; fire-and-forget start without awaiting.","solutions":["Await the factory's StartAsync/CreateAsync before calling GetStartedApplication, e.g. var app = await factory.StartAsync(...); then use app or factory.GetStartedApplication().","Inspect earlier logs/exceptions: if startup failed, the real error will have surfaced via the factory's exception channel — fix that first.","Restructure tests so the started application is obtained inside the async test method rather than a constructor.","Increase the startup timeout via TestingOptions/Environment (e.g. Aspire hosting test timeout env var) if startup is merely slow."],"exampleFix":"// before\nvar factory = DistributedApplicationTesting.CreateAsync<Projects.AppHost>();\nvar app = factory.GetStartedApplication(); // not initialized yet\n// after\nvar factory = await DistributedApplicationTesting.CreateAsync<Projects.AppHost>();\nawait factory.StartAsync();\nvar app = factory.GetStartedApplication();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"var app = await appHost.StartAsync(); // always await before GetStartedApplication\n// if you must poll:\ntry\n{\n    var started = appHost.GetStartedApplication();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"has not been initialized\"))\n{\n    // startup not complete or failed; await start task or inspect startup exception\n}","preventionTips":["Always await CreateAsync/StartAsync before accessing the application or resources.","Never call GetStartedApplication from constructors or field initializers.","If startup is slow, raise the configured startup timeout instead of polling."],"tags":["initialization","async","lifecycle","testing"],"backgroundTag":"invalid-state-transition","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}