System.ArgumentException: An item with the same key has already been added.

Pramod Tolani 25 Reputation points
2024-01-25T11:50:30.97+00:00

Hi guys, I hope you all are doing well. I am working on a project that is based on Azure APIM with a Synthetic GraphQL endpoint. Initially, it worked smoothly with limited data, but after deploying it to production with a larger dataset, it is failing sometimes with an error. I'm sharing the GraphQL schema and relevant information below. Please review:

# Other schema part ...

type AllBranchData {
  message: String
  data: [Branch!]
}
type Branch {
  id: Int
  name: String
  colorSchema: BranchColorSchema
  # Other fields ...
}
type BranchColorSchema {
  logo: String
  background: String
  # Other fields ...
}

# Query:
type Query {
	getAllBranches: AllBranchData!
}

# Other schema part ...

Above is the schema snippet which consists of a query and related types. Here we have created a resolver for the query and in this, we are fetching the data from an APIM API. We have another API for the colorSchema field which fetches data from another APIM API. Both APIs are also located in the same account and this is the 3rd GraphQl endpoint.

Now, I am sharing the resolver of the colorSchema field:

<http-data-source>
    <http-request>
        <set-method>GET</set-method>
        <set-url>@($"https://example.net/api/mockBranchData?qtype=branch_color_schema")</set-url>
		<set-header name="ocp-apim-subscription-key" exists-action="override">
            <value>xyz</value>
        </set-header>
    </http-request>
    <http-response>
        <set-body>@{
            var body = context.Response.Body.As<JObject>();
            var bodyData = body["data"];
			return bodyData.ToString();
		}</set-body>    
	</http-response>
</http-data-source>

In this, you can see that this field resolver data source is an API, which sends us a data key in the response and we need only this so we have extracted the data and sent it back from here.

Now, I am sharing the actual error that I am facing when the data size from the main query API is around 500KB (this is not a complete response due to security reasons):

{
  "errors": [
    {
      "message": "Error trying to resolve field 'colorSchema'.",
      "locations": [
        {
          "line": 4,
          "column": 4
        }
      ],
      "path": ["getAllBranches", "data", 56, "colorSchema"],
      "extensions": {
        "code": "ARGUMENT",
        "codes": ["ARGUMENT"]
      }
    }
  ],
  "data": {
    "getAllBranches": {
      "data": [
        {
          "colorSchema": {
            "logo": "test",
            "background": "test"
          },
          "id": 8101
        },
        {
          "colorSchema": {
            "logo": "",
            "background": "
          },
          "id": 8103
        },
        {
          "colorSchema": null,
          "id": 8104
        }
      ]
    }
  }
}

In the above error, you can see that sometimes we are getting null instead of object and when I tried with the trace then found the following error:

{
    "ClassName": "System.ArgumentException",
    "Message": "An item with the same key has already been added.",
    "Data": {},
    "InnerException": null,
    "HelpURL": null,
    "StackTraceString": "   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)\r\n   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)\r\n   at Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.Policies.Transform.PipelineMessageExtensions.SetBodyPolicyMessageProcess(IPipelineMessage message, IPipelineContext context, String processedContent, CancellationToken cancellation)\r\n   at Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.Policies.Transform.SetBodyValueHandler.ProcessAsync(IPipelineContext context, CancellationToken cancellation)\r\n   at Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.Policies.PipelineWalker.
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,447 questions
{count} votes

Accepted answer
  1. JananiRamesh-MSFT 29,261 Reputation points
    2024-01-31T14:48:25.43+00:00

    @Pramod Tolani Thanks for reaching out. I had a discussion internally and confirmed it's a bug that's already been fixed and is rolling out in production.

    Fix is already available in some regions with ETA of rolling out everywhere in about a month.

    Workaround is to add the following set-url expression code
    <set-url>@{ return "https://myurl.net/resource?unique-param=" + Guid.NewGuid().ToString(); }</set-url>

    do let me know incase of further queries, I would be happy to assist you.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.