JSON post works in EpiServer 8.0.0 but not on higher versions


We have several EpiServer sites where I was able to successfully implement code that posts JSON to an external monitoring web service. This code works in all the instances of our EpiServer sites that have CMS version 8.0.0. But on any of the following EpiServer CMS 8 versions it fails: 8.8.1, 8.8.2, and 8.10.0. I've also tried this code on an EpiServer 9 instance, and it fails with 9 as well. I've posted additional details at Stack Overflow, in case this is just some syntaxing problem that I've made with the JSON post. But since this works in the 8.0.0 CMS, I suspect there is some breaking change with the later versions. Or perhaps I need to include some DLL reference for the later versions.

Here is the Stack Overflow link.


This code works successfully in EpiServer CMS version 8.0.0:

private async Task SendMaintenanceEvent(object maintenanceEvent)
    string endpoint = "https://OurEndpointURL.com/omitted/";
    string endpointDirectory = "target";

    // Provide basic authorization. Credentials must be base-64 encoded to be recognized.
    string credentials = "AuthCredentialsOmitted";
    string credentialsBase64 = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(credentials));

    // Convert the maintenanceEvent object to consumable JSON.
    string maintenanceEventJson = System.Web.Helpers.Json.Encode(maintenanceEvent);
    StringContent content = new StringContent(maintenanceEventJson, Encoding.UTF8, "application/json");

    using (HttpClient httpClient = new HttpClient())
        httpClient.BaseAddress = new Uri(endpoint);
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentialsBase64);
        HttpResponseMessage response = await httpClient.PostAsJsonAsync(endpointDirectory, content);
        if (!response.IsSuccessStatusCode)
            throw new System.Exception("Error sending maintenance event.");

But if I copy that into the higher versions of EpiServer, then it gets stuck on the line where it executes the PostAsJsonAsync. The post occurs, but then Visual Studio debugger never moves to the next line where it evaluates the response object.

I also tried changing that line from PostAsJsonAsync to just PostAsync, but that failed as well. In that case, Fiddler shows that the post gets sent as text/plain instead of as application/json.

It's puzzling why this code fails in the improved versions of EpiServer. Maybe it's not the version, but something else. The version was one of the common differences that I was able to isolate. Thanks for your help.

Edited, Sep 23, 2016 18:12

Adding ConfigureAwait(false) to the HttpClient fixed this, like so:

HttpResponseMessage httpResponseMessage = await httpClient.PostAsJsonAsync(access.EndpointDirectory, stringContent).ConfigureAwait(false);
Sep 27, 2016 19:12
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.