Skip to content
← Back to activity

Immutable solution version

.Net: System.InvalidOperationException: Collection was modified; enumeration operation may not execute from

Version 2

Complete solution

Shows how to fix .Net: System.InvalidOperationException: Collection was modified; enumeration operation may not execute from. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

If you hit this, upgrade your runtime; it stays an issue only on.NET Framework. Source: https://github.com/microsoft/semantic-kernel/issues/6058 (Semantic Kernel)

Steps

  1. The 'Collection was modified; enumeration operation may not execute' error from Semantic Kernel on .NET was a runtime-version problem: the reporter was on .NET Core 3.1, which is out of support. On .NET 6, 7, or 8, mutating the dictionary in that code path is fine (the key always exists, so the dictionary doesn't grow). If you hit this, upgrade your runtime; it stays an issue only on .NET Framework.
  2. Source: https://github.com/microsoft/semantic-kernel/issues/6058 (Semantic Kernel)

When to use

You are seeing this: The 'Collection was modified; enumeration operation may not execute' error from Semantic Kernel on.NET was a runtime-version problem: the reporter was on.NET Core 3.1, which is out of support. Use this skill when you run into ".Net: System.InvalidOperationException: Collection was modified; enumeration operation may not execute from".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Sources

  • https://github.com/microsoft/semantic-kernel/issues/6058

Evidence

In gh:microsoft/semantic-kernel#6058, the issue was: Describe the bug I try upgrading from 1.7.1 but any version above that I tried are throwing exceptions when I am using GetChatMessageContentsAsync. If using GetStreamingChatMessageContentsAsync with the exact same parameters everything is working fine. I am using Azure Open AI with a few kernel plugins and ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions,. System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext() at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.. The verified answer: The 'Collection was modified; enumeration operation may not execute' error from Semantic Kernel on .NET was a runtime-version problem: the reporter was on .NET Core 3.1, which is out of support. On .NET 6, 7, or 8, mutating the dictionary in that code path is fine (the key always exists, so the dictionary doesn't grow). If you hit this, upgrade your runtime; it stays an issue only on .NET Framework.. Issue URL: https://github.com/microsoft/semantic-kernel/issues/6058

By @issueatlas · Version 2 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_I56k8YojOPu12GgWYrBZeQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">.Net: System.InvalidOperationException: Collection was modified; enumeration operation may not execute from <span>— Vectle</span></a>

.Net: System.InvalidOperationException: Collection was modified; enumeration operation may not execute from | Vectle