Amazon-SP-API-CSharp icon indicating copy to clipboard operation
Amazon-SP-API-CSharp copied to clipboard

An item with the same key has already been added. Key: MarketplaceId

Open ProNotion opened this issue 2 years ago • 4 comments

I keep seeing errors like this in my logs.

System.ArgumentException: An item with the same key has already been added. Key: MarketplaceId
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at FikaAmazonAPI.AmazonSpApiSDK.Runtime.AWSSignerHelper.ExtractCanonicalQueryString(RestRequest request)
   at FikaAmazonAPI.AmazonSpApiSDK.Runtime.AWSSigV4Signer.CreateCanonicalRequest(RestRequest restRequest, String signedHeaders)
   at FikaAmazonAPI.AmazonSpApiSDK.Runtime.AWSSigV4Signer.Sign(RestRequest request, String host)
   at FikaAmazonAPI.Services.TokenGeneration.SignWithSTSKeysAndSecurityTokenAsync(RestRequest restRequest, String host, AmazonCredential amazonCredential, CancellationToken cancellationToken)
   at FikaAmazonAPI.Services.RequestService.ExecuteRequestTry[T](RateLimitType rateLimitType, CancellationToken cancellationToken)
   at FikaAmazonAPI.Services.RequestService.ExecuteRequestAsync[T](RateLimitType rateLimitType, CancellationToken cancellationToken)
   at FikaAmazonAPI.Services.FulFillmentInboundService.GetShipmentItemsByNextTokenAsync(String nextToken, ParameterGetShipmentItems parameterShipmentItems, CancellationToken cancellationToken)

Here is the method in which it occurs. Am I doing something wrong here?

public async Task<InboundShipmentItemList> GetShipmentItemsAsync(string shipmentId)
        {
            try
            {
                var items = await _amazonConnection.FulFillmentInbound.GetShipmentItemsByShipmentIdAsync(shipmentId);
                var nextPageToken = items.NextToken;
                var shipmentItemParams = new ParameterGetShipmentItems()
                {
                    MarketplaceId = MarketPlace.UnitedKingdom.ID,
                    NextToken = nextPageToken,
                    QueryType = Constants.QueryType.SHIPMENT
                };
                while (!string.IsNullOrEmpty(nextPageToken))
                {
                    shipmentItemParams.QueryType = Constants.QueryType.NEXT_TOKEN;
                    var moreItems =
                        await _amazonConnection.FulFillmentInbound.GetShipmentItemsByNextTokenAsync(nextPageToken,
                            shipmentItemParams);
                    items.ItemData.AddRange(moreItems.ItemData);
                    nextPageToken = moreItems.NextToken;
                }

                return items.ItemData;
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Error getting shipment items");
            }

            return null;
        }

ProNotion avatar Apr 06 '23 16:04 ProNotion

Can you please confirm if you already tried to set shipmentItemParams without MarketplaceId ?

RakshaChoudhary avatar Jun 21 '23 17:06 RakshaChoudhary

I also see this when trying to run to async operations at the same time like so:

ParameterGetItemOffers get1 = new ParameterGetItemOffers();
get1.ItemCondition = ItemCondition.New;
get1.Asin = "123456789";
get1.MarketplaceId = MarketPlace.US.ID;

ParameterGetItemOffers get2 = new ParameterGetItemOffers();
get2.ItemCondition = ItemCondition.Used;
get2.Asin = "123456789";
get2.MarketplaceId = MarketPlace.US.ID;

Task<GetOffersResult> task1 = myAmazonConnection.ProductPricing.GetItemOffersAsync(get1);
Task<GetOffersResult> task2 = myAmazonConnection.ProductPricing.GetItemOffersAsync(get2);
Task.WaitAll(task1, task2);

MojoJake avatar Apr 06 '24 21:04 MojoJake

I solved my issue, which is maybe slightly different than the OP. The solution was to create a 2nd AmazonConnection option.

MojoJake avatar Apr 07 '24 14:04 MojoJake

I solved my issue, which is maybe slightly different than the OP. The solution was to create a 2nd AmazonConnection option.

Isn't that more of a workaround than a solution to the underlying problem? Glad it's sorted for you but if I get time I will try and dig into this a but further but I don't think it is a consistent problem for me.

ProNotion avatar Apr 08 '24 05:04 ProNotion