Solr Index Exception: ServiceLocationProvider must be set

When migrating a custom index from Azure Search to Solr, you might encounter the following error:

HTML
ServiceLocationProvider must be set.

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.InvalidOperationException: ServiceLocationProvider must be set.

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:

[InvalidOperationException:  ServiceLocationProvider must be set.]
   CommonServiceLocator.ServiceLocator.get_Current() +109
   Sitecore.ContentSearch.SolrProvider.SolrSearchIndex.InitializeSettings() +14
   Sitecore.ContentSearch.SolrProvider.SolrSearchIndex.InitializeSolr() +16
   Sitecore.ContentSearch.SolrProvider.SolrSearchIndex.InitializeIndexSystem() +47
   Sitecore.ContentSearch.SolrProvider.SolrSearchIndex.Initialize() +41

[InvalidOperationException: The index couldn't be initialized. Please make sure the 'InitializeOnAdd' setting is set to false for the Solr indexes.]
   Sitecore.ContentSearch.SolrProvider.SolrSearchIndex.Initialize() +166
   Sitecore.ContentSearch.ContentSearchConfiguration.AddIndex(ISearchIndex index) +329

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
   System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +132
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +146
   Sitecore.Configuration.DefaultFactory.AssignProperties(Object obj, Object[] properties) +853
   Sitecore.Configuration.DefaultFactory.AssignProperties(XmlNode configNode, String[] parameters, Object obj, Boolean assert, Boolean deferred, IFactoryHelper helper) +641
   Sitecore.Configuration.DefaultFactory.CreateObject(XmlNode configNode, String[] parameters, Boolean assert, IFactoryHelper helper) +326
   Sitecore.Configuration.DefaultFactory.CreateObject(XmlNode configNode, String[] parameters, Boolean assert) +72
   Sitecore.Configuration.DefaultFactory.CreateObject(String configPath, String[] parameters, Boolean assert) +703
   Sitecore.ContentSearch.ContentSearchManager.get_SearchConfiguration() +309
   System.Lazy`1.CreateValue() +734
   System.Lazy`1.LazyInitValue() +189
   Sitecore.ContentSearch.DefaultSearchIndexes.get_CsConfig() +16
   Sitecore.ContentSearch.DefaultSearchIndexes.GeAllIndexes() +11
   Sitecore.ContentSearch.SolrProvider.SolrNetIntegration.DefaultSolrStartUp..ctor(BaseSearchIndexes indexes) +59
   Sitecore.ContentSearch.SolrProvider.Pipelines.Loader.InitializeSolrProvider.Process(PipelineArgs args) +72
   (Object , Object ) +9
   Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args) +1450
   Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists) +236
   Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain) +22
   Sitecore.Nexus.Web.HttpModule.Application_Start() +146
   Sitecore.Nexus.Web.HttpModule.Init(HttpApplication app) +918
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +584
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +168
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +277
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +369

[HttpException (0x80004005): Exception has been thrown by the target of an invocation.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +532
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +111
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +724

Here’s the tip:

XML
[InvalidOperationException: The index couldn't be initialized. Please make sure the 'InitializeOnAdd' setting is set to false for the Solr indexes.]

But it isn’t too useful if this is new to you. 😉

Ok, let’s understand:

This error typically comes when the initializeOnAdd property for a Solr index is set to true in your index configuration. This config instructs the index to attempt initialization during application startup, which can conflict with Solr or Sitecore’s dependency setup, especially when ServiceLocationProvider is not yet available.

I think the best approach to resolve this issue is to remove or set the initializeOnAdd property to false for all Solr indexes in your configuration. This ensures that the indexes are not automatically initialized during startup, avoiding the dependency issue.

  1. Locate the Configuration File
    • Identify the specific configuration file that defines the Solr index settings. Typically, it would be located in the /App_Config/Include folder under your custom folder configuration and should have its name like “…DefaultIndexConfiguration.config”.
  2. Edit the Configuration
    • Open the file and locate for: <initializeOnAdd>true</initializeOnAdd>
  3. Remove the Property ✂️
  4. Save the Configuration
  5. Restart Sitecore!

Here is a generic example of a Solr index configuration with the initializeOnAdd property:

XML
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <indexConfigurations>
        <customSolrIndexConfiguration ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration">
          <!-- Here's the culprit -->
          <initializeOnAdd>true</initializeOnAdd>          
        </customSolrIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

When initializeOnAdd is set to true, the index is initialized during startup. If Solr’s dependencies are not fully registered or available at that time, Sitecore throws the ServiceLocationProvider must be set exception. Setting initializeOnAdd to false ensures the initialization process is deferred until it is safe to execute.

  • If running custom indexes on Docker:
    • Verify if the Solr container is running properly. Check the connection and port!
    • Verify if the cores are properly created and check for any typo.
    • Review if you added <param desc="core">$(id)</param> to the config file.
  • Custom Indexes: If you have multiple custom Solr indexes, verify all their configurations for the initializeOnAdd property.

That’s all folks! 💪

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.