{"record":{"id":"2392a01852578dea","repo":"microsoft/semantic-kernel","slug":"client","errorCode":null,"errorMessage":"client","messagePattern":"client","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/Copilot/CopilotStudioAgentThread.cs","lineNumber":28,"sourceCode":"\nnamespace Microsoft.SemanticKernel.Agents.Copilot;\n\n/// <summary>\n/// Represents a conversation thread for a <see cref=\"CopilotStudioAgent\"/>.\n/// </summary>\npublic sealed class CopilotStudioAgentThread : AgentThread\n{\n    private readonly CopilotClient _client;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"CopilotStudioAgentThread\"/> class.\n    /// </summary>\n    /// <param name=\"client\">A client used to interact with the Copilot Agent runtime service.</param>\n    /// <param name=\"conversationId\">An optional session Id to continue an existing session.</param>\n    /// <exception cref=\"ArgumentNullException\"></exception>\n    public CopilotStudioAgentThread(CopilotClient client, string? conversationId = null)\n    {\n        this._client = client ?? throw new ArgumentNullException(nameof(client));\n        this.Id = conversationId;\n    }\n\n    internal ILogger Logger { get; init; } = NullLogger.Instance;\n\n    /// <inheritdoc />\n    protected override async Task<string?> CreateInternalAsync(CancellationToken cancellationToken)\n    {\n        try\n        {\n            await foreach (IActivity activity in this._client.StartConversationAsync(emitStartConversationEvent: true, cancellationToken).ConfigureAwait(false))\n            {\n                if (activity.Conversation is not null)\n                {\n                    return activity.Conversation.Id;\n                }\n            }\n","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/Copilot/CopilotStudioAgentThread.cs#L10-L46","documentation":"This is a constructor guard on CopilotStudioAgentThread. The thread requires a CopilotClient to start conversations and ask questions via the Copilot Studio runtime. A null client would cause NullReferenceException during any subsequent operation, so the constructor fails fast with ArgumentNullException naming 'client'.","triggerScenarios":"Instantiating `new CopilotStudioAgentThread(null)` or passing a null CopilotClient (e.g., from a factory that returned null, or a DI resolution that failed). The guard fires before any service interaction.","commonSituations":"The CopilotClient was never constructed; a factory/builder returned null under an error path; the client was conditionally set and the branch that left it null was taken; unit test instantiation with a null mock.","solutions":["Construct a valid CopilotClient (with the correct Copilot Studio endpoint/settings) and pass the non-null instance to the constructor.","If using DI, register the CopilotClient and inject it into the component that creates threads.","Add a null-check in your factory/build code so the thread is never created with a null client.","In tests, pass a mocked CopilotClient instance (non-null) rather than null."],"exampleFix":"// before\nvar thread = new CopilotStudioAgentThread(client: null, conversationId);\n\n// after\nvar client = new CopilotClient(settings); // or resolved from DI\nvar thread = new CopilotStudioAgentThread(client, conversationId);","handlingStrategy":"validation","validationCode":"CopilotClient? client = ResolveCopilotClient();\nif (client is null)\n    throw new InvalidOperationException(\"CopilotClient could not be resolved; check configuration.\");\nvar thread = new CopilotStudioAgentThread(client, conversationId);","typeGuard":"static bool IsValidCopilotClient(CopilotClient? c) => c is not null;","tryCatchPattern":"try { var thread = new CopilotStudioAgentThread(client, conversationId); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"client\")\n{\n    logger.LogError(\"CopilotClient was null. Ensure the client is constructed/configured before creating the thread.\");\n    throw;\n}","preventionTips":["Construct/configure the CopilotClient in one place and ensure it is non-null before thread creation.","Register the CopilotClient in DI and inject it into the component that builds threads.","In tests, pass a non-null mocked CopilotClient instance."],"tags":["copilot","agents","null-argument","constructor","csharp"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}