I'm in big trouble. The execution time of my multiple requests in the code is being longer than the azure app timeout. I need to update a lot of records and in the end return some data to the web site in azure. I'm sending batches of 200 requests to update 200 records. I need a faster way to batch update records.
My code:
public static Boolean BulkUpdateNoSorteado(CrmServiceClient service, EntityCollection entities)
{
// Create an ExecuteMultipleRequest object.
var multipleRequest = new ExecuteMultipleRequest()
{
// Assign settings that define execution behavior: continue on error, return responses.
Settings = new ExecuteMultipleSettings()
{
ContinueOnError = false,
ReturnResponses = true
},
// Create an empty organization request collection.
Requests = new OrganizationRequestCollection()
};
try
{
var countRequest = Int32.Parse(ConfigurationManager.AppSettings["requestCount"]);
// Add a UpdateRequest for each entity to the request collection.
foreach (var entity in entities.Entities)
{
SetStateRequest request = new SetStateRequest
{
EntityMoniker = new EntityReference(entity.LogicalName, entity.Id),
State = new OptionSetValue(1),
Status = new OptionSetValue((int)Domain.Enum.EnumStatusTicket.Nao_sorteado)
};
multipleRequest.Requests.Add(request);
if (multipleRequest.Requests.Count == countRequest || entity == entities.Entities.Last())
{
if (service.OrganizationServiceProxy == null)
{
service = FactoryGetService.AccessTokenGeneratorAsync();
}
ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)service.Execute(multipleRequest);
multipleRequest = new ExecuteMultipleRequest()
{
// Assign settings that define execution behavior: continue on error, return responses.
Settings = new ExecuteMultipleSettings()
{
ContinueOnError = false,
ReturnResponses = true
},
// Create an empty organization request collection.
Requests = new OrganizationRequestCollection()
};
}
}
return true;
}
catch (Exception)
{
throw;
}
}
Try to refactor your code to use:
Parallel.ForEach
,Parallel.For
in C# for example or,EntityCollection
in a concurrent queue (ConcurrentQueue<T>
in C# for example).You can test easier for Azure timeout if you configure the number of threads and the batch size of the multiple updates in your solution.
Here's what the code in 2. might look like: