Last active
September 5, 2018 09:00
-
-
Save peta/8fdde491333b1baa8cba1279ef98fc00 to your computer and use it in GitHub Desktop.
Automatically adjust batch-size in ExecuteMultipleRequests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IEnumerable<ExecuteMultipleResponse> RunMultipleRequestInSteps(ICrmConnection crmConnection, IEnumerable<OrganizationRequest> organizationRequestList, ExecuteMultipleRequest executeMultipleRequest, bool retryOnBatchSizeLimit = true) | |
{ | |
// Calculate totalElements, elementToProcess and steps | |
var totalElements = organizationRequestList.Count(); | |
var steps = (totalElements <= MaxBatchRequestSize) ? 1 : Math.Ceiling((double) totalElements / MaxBatchRequestSize); | |
var responseList = new List<ExecuteMultipleResponse>(); | |
for (int i = 0; i < steps; i++) | |
{ | |
// Get current requests to process | |
var requestsToProcess = organizationRequestList.Skip(i * MaxBatchRequestSize).Take(MaxBatchRequestSize).ToList(); | |
executeMultipleRequest.Requests.AddRange(requestsToProcess); | |
try | |
{ | |
var responseWithResults = (ExecuteMultipleResponse) crmConnection.ServiceProxy.Execute(executeMultipleRequest); | |
responseList.Add(responseWithResults); | |
} | |
catch (FaultException<OrganizationServiceFault> exc) | |
{ | |
// Check if the maximum batch size has been exceeded. The maximum batch size is only included in the fault if it | |
// the input request collection count exceeds the maximum batch size. | |
if (exc.Detail.ErrorDetails.Contains("MaxBatchSize")) | |
{ | |
int maxBatchSize = Convert.ToInt32(exc.Detail.ErrorDetails["MaxBatchSize"]); | |
if (maxBatchSize < executeMultipleRequest.Requests.Count) | |
{ | |
// Here you could reduce the size of your request collection and re-submit the ExecuteMultiple request | |
Console.WriteLine("The input request collection contains %0 requests, which exceeds the maximum allowed (%1)", | |
executeMultipleRequest.Requests.Count, maxBatchSize); | |
if (i == 0 && retryOnBatchSizeLimit) | |
{ | |
// Fault was raised during first bulk request, so nothing has been done before => no sideeffects | |
// Lower number of bulked requests and retry | |
MaxBatchRequestSize = maxBatchSize; | |
RunMultipleRequestInSteps(crmConnection, organizationRequestList, executeMultipleRequest, false); | |
} | |
else | |
{ | |
// At least one bulk request was successfully issued before or we did already retry | |
throw; | |
} | |
} | |
else | |
{ | |
// Some unknown error related to maximum batch size | |
throw; | |
} | |
} | |
else | |
{ | |
// Some service fault we don't care about | |
throw; | |
} | |
} | |
finally | |
{ | |
executeMultipleRequest.Requests.Clear(); | |
} | |
} | |
return responseList; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment