azure-libraries-for-net: Error listing AppService certificates using fluent api

From @NicolaiPetri on July 11, 2017 7:46

When fetching the list of certificates for specific subscription I’ll get the following exception: “Unable to deserialize the response.” Microsoft.Rest.SerializationException

The code that fails is var certificates = azure.AppServices.AppServiceCertificates.List();

The full exception is here :

Exception: Microsoft.Rest.SerializationException: Unable to deserialize the response. ---> Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Microsoft.Azure.Management.AppService.Fluent.Models.Page'1[Microsoft.Azure.Management.AppService.Fluent.Models.CertificateInner]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
 To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
 Path '', line 1, position 1.
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(JsonReader reader, Type objectType, JsonContract contract)
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
    at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
    at Microsoft.Rest.Serialization.SafeJsonConvert.DeserializeObject[T](String json, JsonSerializerSettings settings)
    at Microsoft.Azure.Management.AppService.Fluent.CertificatesOperations.<ListWithHttpMessagesAsync>d__5.MoveNext()
    --- End of inner exception stack trace ---
    at Microsoft.Azure.Management.AppService.Fluent.CertificatesOperations.<ListWithHttpMessagesAsync>d__5.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at Microsoft.Azure.Management.AppService.Fluent.CertificatesOperationsExtensions.<ListAsync>d__1.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at Microsoft.Azure.Management.AppService.Fluent.AppServiceCertificatesImpl.<ListInnerAsync>d__4.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at Microsoft.Azure.Management.ResourceManager.Fluent.Core.TopLevelModifiableResources`5.List()
    at Microsoft.Azure.Management.AppService.Fluent.AppServiceCertificatesImpl.Microsoft.Azure.Management.ResourceManager.Fluent.Core.CollectionActions.ISupportsListing<Microsoft.Azure.Management.AppService.Fluent.IAppServiceCertificate>.List()
    at AzureExporter2.ResourceIdCachedService.GetResourceId(IAzure azure, String resourceId, String resourceType, String resourceName) in C:\Projects\Dashboard\azure_exporter\AzureExporter2\ResourceIdCachedService.cs:line 47
    at AzureExporter2.Program.<>c__DisplayClass1_0.<Main>b__1(IAsyncResult ar) in C:\Projects\Dashboard\azure_exporter\AzureExporter2\Program.cs:line 88

I can on request provide the failing and valid json response in a private message.

Copied from original issue: Azure/azure-sdk-for-net#3474

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Hi. Any update on this?

Here’s a workaround:

class FixCertificatesHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        HttpResponseMessage resp = await base.SendAsync(request, cancellationToken);
        if (resp.StatusCode == System.Net.HttpStatusCode.OK && Regex.IsMatch(request.RequestUri.LocalPath, "^/subscriptions/[^/]+/providers/Microsoft.Web/certificates$"))
        {
            var str = await resp.Content.ReadAsStringAsync();
            str = "{ \"value\": " + str + " }";
            resp.Content.Dispose();
            resp.Content = new StringContent(str);
        }
        return resp;
    }
}

and then use Azure.Configure().WithDelegatingHandler(new FixCertificatesHandler())