Instantiate client in a non-Sitecore context
In container-based environments, the xConnect API is not publicly exposed.
The xConnect Client API is portable and can be used in a non-Sitecore context. The following example demonstrates how to instantiate the client from a console application.
Only trusted clients with the correct client certificates can communicate with xConnect. If you want to write to xConnect from a mobile application, you must set up an intermediary server that collects data from the application and submits that data to xConnect at regular intervals. You cannot write directly to xConnect from a mobile device.
Do not initialize the XConnectConfiguration
object more than one time to avoid unnecessary resource consumption and potential conflicts.
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)
{
try
{
// Use the singleton configuration instance
var cfg = XConnectConfiguration.Instance;
using (var client = new XConnectClient(cfg))
{
// Use the client here
}
}
catch (XdbModelConflictException ce)
{
Console.WriteLine("ERROR:" + ce.Message);
return;
}
}
}
// Singleton class for XConnectClientConfiguration
public static class XConnectConfiguration
{
private static readonly Lazy<XConnectClientConfiguration> _instance =
new Lazy<XConnectClientConfiguration>(() => InitializeConfiguration());
public static XConnectClientConfiguration Instance => _instance.Value;
private static XConnectClientConfiguration InitializeConfiguration()
{
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(0, 0, 20));
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);
// Initialize the configuration synchronously for simplicity
cfg.InitializeAsync().GetAwaiter().GetResult();
return cfg;
}
}
}
Using a custom model
In a Sitecore context, the xConnect Client API uses a runtime model that is assembled from all the models available in configuration. In a non-Sitecore context, you must pass the model that you want to work with into XConnectClientConfiguration
:
var cfg = new XConnectClientConfiguration(
new XdbRuntimeModel(Custom.Namespace.Model), collectionClient, searchClient, configurationClient);
You can instantiate an XdbRuntimeModel
object with an array of models.