The given key was not present in the dictionary

143 views
Skip to first unread message

abhay dhar

unread,
Jan 13, 2020, 4:41:50 AM1/13/20
to SolrNet
Hi , we are using SolrNet version 1.0.13.0 in Sitecore CMS , which is a .NET web application on 4.7.2 dotnet framework. We are initliazing the SolrNet using Startup.Init as shown below. 

Startup.Init<T>(loggingSolrConnection);

But we are getting this error. I would be greatful if you guys could help and provide any inputs.


The given key was not present in the dictionary.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[KeyNotFoundException: The given key was not present in the dictionary.]
   System.ThrowHelper.ThrowKeyNotFoundException() +36
   System.Collections.Generic.Dictionary`2.get_Item(TKey key) +52
   SolrNet.Utils.Container.DoGetInstance(Type serviceType, String key) +25
   CommonServiceLocator.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) +54

[ActivationException: Activation error occurred while trying to get instance of type ISolrMoreLikeThisHandlerQueryResultsParser`1, key ""]
   CommonServiceLocator.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) +155
   CommonServiceLocator.ServiceLocatorImplBase.GetInstance() +54

Som Phouangpraseuth

unread,
Jan 14, 2020, 5:52:11 AM1/14/20
to SolrNet
Hi Abhay,

Firstly, you must create a Document class that you would use for the <T>

For instance,

Document.cs


 
  public class Document
   
{
       
[SolrUniqueKey("id")]
       
public string Id { get; set; }


       
[SolrUniqueKey("content")]
       
public string Content { get; set; }


       
[SolrField("creationdate")]
       
public DateTime CreationDate { get; set; }
   
}



Then when you do the initialized, run below in the service class, your SolrURL string like "http://localhost:8983/solr" if you are running it locally via docker or binary directly for example.


   
 Startup.Init<Document>(yourSolrURLstringHere));

 
// Done connect to local Solr Server
 
ISolrOperations<Document>  solr = Startup.Container.GetInstance<ISolrOperations<Document>>();

abhay dhar

unread,
Jan 16, 2020, 4:31:30 AM1/16/20
to SolrNet
Hi Som,
Two clarifications on your reply

1- SolrNet - we are initializing with generic type model T , Can we not use that ? , the code base which has solrnet is being upgraded to dotnet 4.7.1 and on the previous version it was working with the generic type.

2- If you see the stack trace , it is pointing to CommonServiceLocator , and the messages below this stack trace are arising from the internal Sitecore(.NET) CMS application. The Sitecore .NET application also initializes its own ServiceLocator(unfortunately that code is closed source)
Could that cause some issues ?

3- The stack trace throws the error "Key not found" , but the stack trace message is showing Key as blank.


 Startup.Init<Document>(yourSolrURLstringHere)); 

Som Phouangpraseuth

unread,
Jan 17, 2020, 12:29:44 PM1/17/20
to SolrNet
Hi Abhay,

In order to use the SolrNet, you really need to understand the "fields" you have setup with the Solr Collection you are trying to run query against.  
For instance, access your Solr Admin via a browser or you may use `curl` from linux command line.

Example below screen capture shows running query for term *:*  from Solr collection index `techproducts` and it return fields like "id", "cat", "name", "price", etc... 

These would be add to your specific code for the Class Document or any name you wish your

public class Document
   
{
 
       
[SolrUniqueKey("id")]
       
public string Id { get; set; }




       
[SolrUniqueKey("cat")]
       
public string Category { get; set; }



       
[SolrUniqueKey("name")]
       
public string Name { get; set; }

...


If you want to consider all of the fields from the `techproducts` schema, then do that with each of the fields in your Solr Collecton `techproducts` schema response.  You don't have to reference all the fields in your SolrNet "Document" class, only the one you would use with your application.


Solr Quick Start: techproducts Query screen with results

abhay dhar

unread,
Jan 20, 2020, 8:02:17 AM1/20/20
to SolrNet
Hi Som,

Why are you always referring to having a parent class , i am calling Startup.Init in a base class and from derived class I am passing the concrete glass.

I am again reiterating that the code is working fine in another instance.

Som Phouangpraseuth

unread,
Jan 20, 2020, 11:01:59 AM1/20/20
to SolrNet
What are the "Key" that you defined that works for you?  It is the Solr Fields (the keys) is what I am trying to point out!  I was trying to demonstrate the basic use of how to get SolrNet working, maybe it's better you referred to more details documentation here for basic usage https://github.com/SolrNet/SolrNet/blob/master/Documentation/Basic-usage.md 
Reply all
Reply to author
Forward
0 new messages