Exception: The given key was not present in the dictionary.
Bot Info
- SDK Platform: .NET
- SDK Version: 4.6
- Active Channels:
- Deployment Environment: Azure Bot Service Test Chat and local development with emulator
Issue Description
Whenever I send a message I get always the error
Exception: The given key was not present in the dictionary.
Code Example
I checked the API ID and Key from luis.ai: https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/f2fed607-ce8a-4ea0-9b1c-d0cf2528f61c?subscription-key=f4f613d819fb4118b54226116c9751e8&verbose=true&timezoneOffset=0&q=
So in web.config:
<add key="LuisAppId" value="f2fed607-ce8a-4ea0-9b1c-d0cf2528f61c" />
<add key="LuisAPIKey" value="REMOVED" />
<add key="LuisAPIHostName" value="westus.api.cognitive.microsoft.com" />
BasicLuisDialog.cs
`
using System;
using System.Configuration;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Luis;
using Microsoft.Bot.Builder.Luis.Models;
using System.Collections.Generic;
using System.Text;
namespace Microsoft.Bot.Sample.LuisBot
{
// For more information about this template visit http://aka.ms/azurebots-csharp-luis
[Serializable]
public class BasicLuisDialog : LuisDialog<object>
{
// CONSTANTS
// Entity
public const string Entity_Location = "testchatbotservice.location";
// Intents
public const string Intent_None = "None";
public const string Intent_Help = "testchatbotservice.Help";
public const string Intent_Greeting = "testchatbotservice.Greeting";
public const string Intent_Cancel = "testchatbotservice.Cancel";
public const string Intent_AskWeather = "testchatbotservice.AskWeather";
public BasicLuisDialog() : base(new LuisService(new LuisModelAttribute(
ConfigurationManager.AppSettings["LuisAppId"],
ConfigurationManager.AppSettings["LuisAPIKey"],
domain: ConfigurationManager.AppSettings["LuisAPIHostName"])))
{
}
// Go to https://luis.ai and create a new intent, then train/publish your luis app.
// Finally replace "Gretting" with the name of your newly created intent in the following handler
[LuisIntent(Intent_None)]
public async Task NoneIntent(IDialogContext context, LuisResult result)
{
await this.ShowLuisResult(context, result);
}
[LuisIntent(Intent_Help)]
public async Task HelpIntent(IDialogContext context, LuisResult result)
{
await this.ShowLuisResult(context, result);
}
[LuisIntent(Intent_Greeting)]
public async Task GreetingIntent(IDialogContext context, LuisResult result)
{
await this.ShowLuisResult(context, result);
}
[LuisIntent(Intent_Cancel)]
public async Task CancelIntent(IDialogContext context, LuisResult result)
{
await this.ShowLuisResult(context, result);
}
[LuisIntent(Intent_AskWeather)]
public async Task AskWeatherIntent(IDialogContext context, LuisResult result)
{
await this.ShowLuisResult(context, result);
}
// Entities found in result
public string BotEntityRecognition(string intentName, LuisResult result)
{
IList<EntityRecommendation> listOfEntitiesFound = result.Entities;
StringBuilder entityResults = new StringBuilder();
foreach (EntityRecommendation item in listOfEntitiesFound)
{
// Query: Turn on the [light]
// item.Type = "testchatbotservice.Device"
// item.Entity = "light"
entityResults.Append(item.Type.Replace("testchatbotservice.","") + "=" + item.Entity + ",");
}
// remove last comma
entityResults.Remove(entityResults.Length - 1, 1);
return entityResults.ToString();
}
private async Task ShowLuisResult(IDialogContext context, LuisResult result)
{
// get recognized entities
string entities = this.BotEntityRecognition(Intent_AskWeather, result);
// round number
string roundedScore = result.Intents[0].Score != null ? (Math.Round(result.Intents[0].Score.Value, 2).ToString()) : "0";
await context.PostAsync($"**Query**: {result.Query}, **Intent**: {result.Intents[0].Intent}, **Score**: {roundedScore}. **Entities**: {entities}");
context.Wait(MessageReceived);
}
}
}
`
Expected Behavior
When I try the app in the luis.ai site test, it shows the result correctly
Actual Results
I ge the following error:
`
"membersAdded": [],
"membersRemoved": [],
"locale": "en-US",
"text": "Exception: The given key was not present in the dictionary.",
"attachments": [
{
"contentType": "text/plain",
"content": " at System.Collections.Generic.Dictionary2.get_Item(TKey key)\r\n at Microsoft.Bot.Builder.Dialogs.LuisDialog1.1.<MessageReceived>d__8.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Dialogs.Internals.DialogTask.ThunkResume1.2.<Microsoft-Bot-Builder-Internals-Fibers-IWait<C>-PollAsync>d__19.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Internals.Fibers.Frame1.<Microsoft-Bot-Builder-Internals-Fibers-IFrameLoop1.<Microsoft-Bot-Builder-Internals-Fibers-IFiberLoop<C>-PollAsync>d__16.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at Microsoft.Bot.Builder.Internals.Fibers.Wait2.Microsoft.Bot.Builder.Internals.Fibers.IAwaiter1.<ResumeAsync>d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Dialogs.Internals.DialogTask.ThunkResume1.2.<Microsoft-Bot-Builder-Internals-Fibers-IWait<C>-PollAsync>d__19.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Internals.Fibers.Frame1.<Microsoft-Bot-Builder-Internals-Fibers-IFrameLoop1.<Microsoft-Bot-Builder-Internals-Fibers-IFiberLoop<C>-PollAsync>d__16.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Dialogs.Internals.DialogTask.<Microsoft-Bot-Builder-Base-IEventLoop-PollAsync>d__23.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Dialogs.Internals.ReactiveDialogTask.<Microsoft-Bot-Builder-Base-IEventLoop-PollAsync>d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Dialogs.Internals.ScoringEventLoop1.
`
Code failed while searching for entities. Added an "added" variable to check if the Remove method should be called.
// Entities found in result
public string BotEntityRecognition(string intentName, LuisResult result)
{
IList<EntityRecommendation> listOfEntitiesFound = result.Entities;
StringBuilder entityResults = new StringBuilder();
bool added = false;
foreach (EntityRecommendation item in listOfEntitiesFound)
{
// Query: Turn on the [light]
// item.Type = "testchatbotservice.Device"
// item.Entity = "light"
entityResults.Append(item.Type.Replace("testchatbotservice.", "") + "=" + item.Entity + ",");
added = true;
}
// remove last comma
if (added)
entityResults.Remove(entityResults.Length - 1, 1);
return entityResults.ToString();
}