3

When I try to initialize my XConnectClientConfiguration when running on an Azure App Service, the call to cfg.InitializeAsync() simply hangs. The controller trying to connect to XConnect eventually throws a 500-Timeout.

This same code runs fine when used in a console app.

public static async Task<XConnectClient> CreateXConnectClient(string connectionString, Uri serviceUri)
{
  var options = CertificateWebRequestHandlerModifierOptions.Parse(connectionString);
  options.AllowInvalidClientCertificates = "true";

  var certificateModifiers = new[] { new CertificateWebRequestHandlerModifier(options) };

  var clientModifiers = new[] { new TimeoutHttpClientModifier(new TimeSpan(0, 0, 30)) };

  var collectionClient = new CollectionWebApiClient(
    new Uri(serviceUri, "odata/"),
    clientModifiers, certificateModifiers);
  var searchClient = new SearchWebApiClient(
    new Uri(serviceUri, "odata/"),
    clientModifiers, certificateModifiers);
  var configurationClient = new ConfigurationWebApiClient(
    new Uri(serviceUri, "configuration/"),
    clientModifiers, certificateModifiers);

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

  await cfg.InitializeAsync();
  var client = new XConnectClient(cfg);

  return client;
}

I've validated that I can connect to the XConnect instance from the App Service using the same certificate:

using (var handler = new HttpClientHandler())
{
  var certificate = GetCert();
  handler.ClientCertificates.Add(certificate);
  using (var client = new HttpClient(handler))
  {
    client.BaseAddress = Settings.XConnectServiceUri;
    var response = client.GetAsync("odata").Result;
    Show(response.IsSuccessStatusCode
      ? response.Content.ReadAsStringAsync().Result
      : $"Error, received status code {response.StatusCode}: {response.ReasonPhrase}");
  }
}

Any ideas?

4
  • hey Andy can you try in this way : using (var client = SitecoreXConnectClientConfiguration.GetClient()) . You don't need so much code if you want to access client in Sitecore Context Sep 9, 2018 at 16:10
  • Thanks @VladIobagiu. But that’s why I’m doing it this way ... I’m outside of Sitecore context. This is an external application reporting interactions to xDB from an Azure app service.
    – Andy Uzick
    Sep 9, 2018 at 22:39
  • Does the xConnect server have AllowInvalidClientCertificates = true? Sep 11, 2018 at 0:34
  • Interesting thought about the invalid cert. When I set that to "false", the cert does load (cfg.InitializeAsync() throws a cert not found error). I don't know what the server setting is. I'll investigate. I didn't think about that since the exact same logic works with the same target xConnect server when called from a console application instead of a web app. Can you expand on how that "allow invalid" setting might be affecting this?
    – Andy Uzick
    Sep 11, 2018 at 14:00

1 Answer 1

3

Turns out the problem had nothing to do with the client certificate. It seems that when running under IIS, it's imperative that the controller's methods all run async when using initializing the XConnectClient. Not all of mine were.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.