Configuring xConnect Client API timeouts

Version: 10.3

You can set a default timeout when you instantiate the xConnect Client API or you can set a timeout for a particular request.

Set the default client timeout in a non-Sitecore context

In a non-Sitecore context, default client timeout can be set by passing in a TimeoutHttpClientModifier. In the following example, a timeout modifier is added to the search, collection, and configuration clients before the configuration is initialized:

RequestResponse
using Sitecore.XConnect;
using Sitecore.XConnect.Client;
using Sitecore.XConnect.Schema;
using System;
using System.Threading.Tasks;
using Sitecore.XConnect.Client.WebApi;
using Sitecore.Xdb.Common.Web;
using Sitecore.XConnect.Collection.Model;
using System.Collections.Generic;

namespace Symposium.xConnect.Course
{
    public class Program
    {
        private static void Main(string[] args)
        {
            MainAsync(args).ConfigureAwait(false).GetAwaiter().GetResult();
        }

        private static async Task MainAsync(string[] args)
        {
           CertificateHttpClientHandlerModifierOptions options = CertificateHttpClientHandlerModifierOptions.Parse("StoreName=My;StoreLocation=LocalMachine;FindType=FindByThumbprint;FindValue=15E6693B0AECB63DE57D991EC363CA462DC52432");

            var certificateModifier = new CertificateHttpClientHandlerModifier(options);

            List<IHttpClientModifier> clientModifiers = new List<IHttpClientModifier>();
            var timeoutClientModifier = new TimeoutHttpClientModifier(new TimeSpan(200));
            clientModifiers.Add(timeoutClientModifier);

            var collectionClient = new CollectionWebApiClient(new Uri("https://sc900rev170622_xconnect/odata"), clientModifiers, new[] { certificateModifier });
            var searchClient = new SearchWebApiClient(new Uri("https://sc900rev170622_xconnect/odata"), clientModifiers, new[] { certificateModifier });
            var configurationClient = new ConfigurationWebApiClient(new Uri("https://sc900rev170622_xconnect/configuration"), clientModifiers, new[] { certificateModifier });

            var cfg = new XConnectClientConfiguration(
                new XdbRuntimeModel(CollectionModel.Model), collectionClient, searchClient, configurationClient);

            try
            {
                await cfg.InitializeAsync();

            }
            catch (XdbModelConflictException ce)
            {
                Console.WriteLine("ERROR:" + ce.Message);
                return;
            }

            using (var client = new XConnectClient(cfg))
            {
            }
        }
    }
}
Note

If you do not specify a client timeout, the default client timeout is 100 seconds.

Set the default client timeout in a Sitecore context

There is no default client timeout for the xConnect Client API in a Sitecore context. This is because different features within the platform have different timeout requirements. For example, the tracker must time out quickly if a contact is not available whereas the List Manager must allow time for large batches to be processed.

Specify a timeout per request

You can specify a timeout per request by passing in a cancellation token. All extension methods on the client have an overload that accepts a cancellation token:

RequestResponse
// GET
var cancellationTokenSource = new CancellationTokenSource(200);
Task<Contact> contactTask = client.GetAsync<Contact>(reference, new ContactExecutionOptions(new ContactExpandOptions() { }), cancellationTokenSource.Token);

// SUBMIT
var cancellationTokenSource1 = new CancellationTokenSource(200);
await client.SubmitAsync(cancellationTokenSource1.Token);

// GETBATCHENUMERATOR
var results = await client.Contacts.Where(f => f.IsKnown == true).GetBatchEnumerator(10, cancellationTokenSource.Token);

// TOSEARCHRESULTS
var results = await client.Contacts.Where(f => f.IsKnown == true).ToSearchResults(cancellationTokenSource.Token);
Tip

If you are using the synchronous extension methods, you can pass in a TimeSpan.

Do you have some feedback for us?

If you have suggestions for improving this article,