{"record":{"id":"d4e2f1473048282e","repo":"microsoft/aspire","slug":"an-interaction-id-is-required-when-uploading-a-file","errorCode":null,"errorMessage":"An interaction ID is required when uploading a file.","messagePattern":"An interaction ID is required when uploading a file\\.","errorType":"validation","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/Backchannel/AppHostRpcTarget.cs","lineNumber":263,"sourceCode":"        await activityReporter.CompleteInteractionAsync(promptId, answers, updateResponse: true, cancellationToken).ConfigureAwait(false);\n    }\n\n    /// <summary>\n    /// Registers a local file in the upload store by copying it to a managed temp location.\n    /// Returns the file ID that can be used to reference the file in interaction responses.\n    /// </summary>\n    public async Task<UploadFileResponse> UploadFileAsync(UploadFileRequest request, CancellationToken cancellationToken = default)\n    {\n        var maxUploadSize = FileUploadHelpers.GetMaxFileUploadSize(configuration);\n\n        if (request.Data.Length > maxUploadSize)\n        {\n            throw new InvalidOperationException($\"File '{request.FileName}' exceeds the maximum upload size of {maxUploadSize} bytes.\");\n        }\n\n        if (request.InteractionId <= 0)\n        {\n            throw new InvalidOperationException(\"An interaction ID is required when uploading a file.\");\n        }\n        if (string.IsNullOrEmpty(request.InputName))\n        {\n            throw new InvalidOperationException(\"An input name is required when uploading a file.\");\n        }\n\n        var (fileId, filePath) = fileUploadStore.CreateEntry(request.FileName, request.InteractionId, request.InputName);\n\n        try\n        {\n            var destStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 81920, useAsync: true);\n            await using (destStream.ConfigureAwait(false))\n            {\n                await destStream.WriteAsync(request.Data, cancellationToken).ConfigureAwait(false);\n            }\n        }\n        catch\n        {","sourceCodeStart":245,"sourceCodeEnd":281,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/Backchannel/AppHostRpcTarget.cs#L245-L281","documentation":"UploadFileAsync requires the upload to be associated with an active interaction; it throws InvalidOperationException when request.InteractionId is zero or negative. The uploaded file must be attached to a pending file-upload interaction identified by that id.","triggerScenarios":"Calling UploadFileAsync with a request whose InteractionId was never set (default 0) or is a stale/negative value — e.g., constructing UploadFileRequest without an interaction context.","commonSituations":"Uploading a file outside of an actual file-upload interaction flow; reusing a request object whose interaction id field defaulted to 0; an interaction that already completed so the stored id is no longer valid/positive in caller state.","solutions":["Set InteractionId to the id of the pending file-upload interaction that requested the file.","Only call UploadFileAsync from within the interaction callback where the interaction id is available.","Verify the id was copied correctly from the interaction payload (not left at the default 0)."],"exampleFix":"// before\nawait rpc.UploadFileAsync(new UploadFileRequest { FileName = f, Data = bytes }); // InteractionId = 0\n// after\nawait rpc.UploadFileAsync(new UploadFileRequest { FileName = f, Data = bytes, InteractionId = interaction.Id, InputName = \"file\" });","handlingStrategy":"validation","validationCode":"if (request.InteractionId <= 0)\n{\n    throw new InvalidOperationException(\"Set InteractionId from the pending file-upload interaction before uploading.\");\n}","typeGuard":"static bool HasInteractionId(UploadFileRequest r) => r.InteractionId > 0;","tryCatchPattern":"try\n{\n    await rpc.UploadFileAsync(request, cancellationToken);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"interaction ID is required\"))\n{\n    // rebuild the request with the active interaction's id\n}","preventionTips":["Only build UploadFileRequest inside the file-upload interaction callback where the id is in scope.","Never rely on the default InteractionId of 0; copy it explicitly from the interaction object."],"tags":["rpc","interactions","missing-argument"],"backgroundTag":"missing-required-argument","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"}