Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"The given key was not present in the dictionary" error

See original GitHub issue

HI there,

I would like your help please. I’ve reported this issue at codeplex but I’ve realised I should have raised it here. Apologises.

I’ve created an IVR script on a webserver. It keeps failing randomly. To find out the issue, I wrote a CurrentDomain_UnhandledException. It prints out a message and the source of the error.

It gave this error.

The given key was not present in the dictionary. 

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at AsterNET.FastAGI.AGIConnectionHandler.Run() in e:\Projects\Codeplex\AsterNET\Asterisk.2013\Asterisk.NET\FastAGI\AGIConnectionHandler.cs:line 103
   at AsterNET.Util.ThreadTask.Run() in e:\Projects\Codeplex\AsterNET\Asterisk.2013\Asterisk.NET\Util\ThreadTask.cs:line 36
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

This is how I configured the agiServer component before starting it.

int PortNumber = ToolboxHelper.getSettingByNameAsInt("PortNumber");

            AsteriskFastAGI agiServer=new AsteriskFastAGI
                MappingStrategy = new GeneralMappingStrategy(
                    new List<ScriptMapping>()
                        new ScriptMapping()
                            ScriptName = "callmonitor", // script name - passed by the Asterisk server
                            ScriptClass = "AsteriskAGI.CallMonitorAGI" // code to run for this script

            agiServer.BindPort = PortNumber;

                agiServer.Start(); // Start the service. This runs synchronously - ctrl+c to exit

            catch (Exception ex)


This error is occuring in the AsterNET code.

What’s happening in AGIConnectionHandler.cs at line 103? What key is it expecting? This happens randomly, perhaps also when a call has connected with my script.

Am I missing something in my setup?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:10

github_iconTop GitHub Comments

jjowenscommented, Jan 20, 2015

I’ve downloaded the source and made some changes to find the root of the issue. Apologises for the long post.

In AGIConnectionHandler.cs, around lines 93, where it throws an exception. I commented out the “throw ex” and got it to write the error message.

catch (AGIException ex)
                logger.Error("AGIException while handling request", ex);
                //throw ex;
                              writeToLog("AGiConnectionError: AGIException:" + ex.ToString());

I found where the error was occuring.

AGiConnectionError: Ex:System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at AsterNET.FastAGI.AGIRequest.get_Script() in SNIP[..]\Asterisk.2013\Asterisk.NET\FastAGI\AGIRequest.cs:line 343
   at AsterNET.FastAGI.MappingStrategies.GeneralMappingStrategy.DetermineScript(AGIRequest request) in SNIP[..]Asterisk.2013\Asterisk.NET\FastAGI\MappingStrategies\GeneralMappingStrategy.cs:line 132
   at AsterNET.FastAGI.AGIConnectionHandler.Run() in SNIP[..]Asterisk.2013\Asterisk.NET\FastAGI\AGIConnectionHandler.cs:line 64

I looked inside AGIRequest.cs for line 343, GeneralMappingStrategy.cs for line 132 and AGIConnectionHandler.cs for line 64.


#region Script()
        /// <summary>
        /// Returns the name of the script to execute.
        /// </summary>
        public string Script
                if (script != null)
                    return script;

                script = ((string)request["network_script"]); // <--- THIS IS LINE 343
                if (script != null)
                    Match scriptMatcher = Common.AGI_SCRIPT_PATTERN.Match(script);
                    if (scriptMatcher.Success)
                        script = scriptMatcher.Groups[1].Value;
                        parameters = scriptMatcher.Groups[2].Value;
                return script;


        public AGIScript DetermineScript(AGIRequest request)
            AGIScript script = null;
            if (mapAssemblies != null)
                lock (mapAssemblies)
                    if (mapAssemblies.ContainsKey(request.Script))
                        script = mapAssemblies[request.Script].CreateInstance(); // THIS IS LINE 132
            return script;


AGIReader reader = new AGIReader(socket);
                AGIWriter writer = new AGIWriter(socket);
                AGIRequest request = reader.ReadRequest();
                AGIChannel channel = new AGIChannel(writer, reader, this._SC511_CAUSES_EXCEPTION, this._SCHANGUP_CAUSES_EXCEPTION);
                AGIScript script = mappingStrategy.DetermineScript(request); // THIS IS LINE 64
                Thread.SetData(, channel);

So what happened, it was trying to find the script from the Dictionary (mapAssemblies) with the “network_script” variable/name so an instance of the script can be created and executed.

What is “network_script”? Is this something I need to declare in my AGIServer setup?

I looked at examples in and I don’t see anything referring to “network_script”, except I didn’t use the Answer(); or HangUp(); in my script. Are they required? I was able to execute the script without those. The random hanging still occurs. Example

 public override void Service(AGIRequest param1, AGIChannel param2)
            Answer(); // Answer the call
// snip
            HangUp(); // End the call
jjowenscommented, Jan 16, 2015

Hi skrusty,

It’s written as a console application and it’s installed on a server. It’s a console applicaiton with framework set to 4.5.

This happens randomly. Is there any more infornation you would like?

Read more comments on GitHub >

github_iconTop Results From Across the Web

The given key was not present in the dictionary. Which key?
This exception is thrown when you try to index to something that isn't there, for example: Dictionary<String, String> test = new ...
Read more >
The given Key was not present in the dictionary
Symptoms. Consider the following scenario: · Cause. There is a faulty registry item in the XML file that is associated with the Group...
Read more >
Solved: The given key was not present in the dictionary
Solved: Hello, I am having a refresh problem and I need help, Currently, I am using Power BI Desktop (January 2021), after applying...
Read more >
[Solved] The given key was not present in the dictionary
Solution 1​​ Hi, Actually this exception occur when you are accessing Dictionary for Incorrect value. Here i have created one code snippet for ......
Read more >
The given key was not present in the dictionary.
Generally 'The given key was not present in the dictionary' implies the VBO has no 'awareness' of the Excel file. The VBO keeps...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found