Configure your own Search & Navigation timeouts

  • Updated

The default request timeout is set to 100 seconds (HttpWebRequest.Timeout default) which could be problematic when requests takes longer than expected.

You might know that you can set the request timeout in configuration for Search & Navigation (Find)

This will be used for all types of Find requests and could pose a problem if you want to lower it to 10 secs but at the same time not affect requests that should be allowed to run longer e.g bulk requests.

Timeout is set in milliseconds.

CMS 11 and lower

<appSettings><add key="episerver:FindDefaultRequestTimeout" value="10000" />

CMS 12

   "AppSettings": {
        "episerver:FindDefaultRequestTimeout": 10000 
    }

More on configuration here https://world.optimizely.com/documentation/developer-guides/CMS/configuration/

But it's also possible with some code to set the timeout for a specific request

Add the following extension method

public static ITypeSearch<TResult> SetTimeout<TResult>(this ISearch<TResult> search, int timeout)
{
    return new Search<TResult, IQuery>(search, context =>
    {
        var existingAction = context.CommandAction;
        context.CommandAction = command =>
        {
            if (existingAction.IsNotNull())
            {
                existingAction(command);
            }
            command.ExplicitRequestTimeout = timeout;
        };
    });
}

And use it like this

var result = SearchClient.Instance
  .UnifiedSearchFor("some search term")
  .SetTimeout(10000)
  .GetResult();


Make sure you catch your timeouts. They will throw a ServiceException.
More on Find exceptions you should consider catching is available in Jonas Bergqvist's blog post Exceptions in find