I have a dictionary with strings as keys, and async functions as the values. It's defined as such:
_messageMap = new Dictionary<string, Func<UpgradeTask, Task>>
{
{ "Upgrade1", Upgrade1 }
};
The functions look like this:
private async Task Upgrade1(UpgradeTask upgradePayload)
{
await _databaseFunctions.DoUpgrade("Upgrade1", upgradePayload.UpgradeId);
}
This is all contained within a class that has an execute method that will call the appropriate function by the string it gets. It essentially functions as a callback mechanism for when an event happens in the future. Execute looks like this:
public async Task Execute(FutureEvent futureEvent)
{
var payLoad = JsonSerializer.Deserialize<UpgradeTask>(futureEvent.Message);
await _messageMap[payLoad.UpgradeId].Invoke(payLoad);
}
This however seems to hang indefinitely if the payload ever had an UpgradeId that's not in the dictionary.
What I expected to have happen is if the UpgradeId exists in the dictionary it will invoke that function. Which works perfectly in that case actually. But what seems to happen if an UpgradeId is in the payload that doesn't exist in the dictionary it hangs indefinitely. It's like it's awaiting something that never happens. I thought it would just skip it or maybe even error out. But it just silently fails and hangs forever. This is a problem because it doesn't actually crash the rest of the app. Everything else seems like it's working fine, but none of the callbacks get processed if there's ever one event that doesn't exist in the dictionary.
Why is this happening? I know I can just check if it exists in the dictionary beforehand. But I'm still very confused on why await just hangs forever at that point. I'd like to understand what I'm doing wrong.
Edit: An example on GitHub: https://github.com/Johnhersh/DictionaryTimerExample This is unfortunately the smallest I could get it. Simplifying it further goes back to giving me the expected behavior of throwing when accessing that key.