/// <summary> /// Gets the max ticket id from the table for new request created. /// </summary> /// <returns>Ticket Id.</returns> public async Task <int> GetTicketIdAsync() { int nextTicketId = 0; try { await this.EnsureInitializedAsync(); TableQuery <TicketIdGenerator> query = new TableQuery <TicketIdGenerator>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, Constants.TicketIdGeneratorPartitionKey)); TableContinuationToken tableContinuationToken = null; do { var queryResponse = await this.CloudTable.ExecuteQuerySegmentedAsync(query, tableContinuationToken); tableContinuationToken = queryResponse.ContinuationToken; var ticketIdGenerator = queryResponse.Results.FirstOrDefault(); if (ticketIdGenerator == null) { ticketIdGenerator = new TicketIdGenerator { MaxTicketId = 1, RowKey = Guid.NewGuid().ToString(), }; TableOperation insertOrMergeOperation = TableOperation.InsertOrReplace(ticketIdGenerator); TableResult result = await this.CloudTable.ExecuteAsync(insertOrMergeOperation); nextTicketId = ticketIdGenerator.MaxTicketId; } else { await this.UpdateTicketIdAsync(ticketIdGenerator); nextTicketId = ticketIdGenerator.MaxTicketId; this.retryCount = 0; } }while (tableContinuationToken != null && query != null); } catch (StorageException ex) { if (ex.RequestInformation.HttpStatusCode == 412) { this.logger.LogError("Optimistic concurrency violation – entity has changed since it was retrieved."); await this.RetryTicketIdGenerationAsync(); } } catch (Exception ex) { this.logger.LogError($"Error {ex.Message}"); throw new Exception(ex.Message); } return(nextTicketId); }
/// <summary> /// update the ticket id in the table storage. /// </summary> /// <param name="ticketIdGenerator"> Entity containing latest ticket Id details.</param> /// <returns> Returns next ticket id generated from table storage.</returns> public async Task <int> UpdateTicketIdAsync(TicketIdGenerator ticketIdGenerator) { if (ticketIdGenerator != null) { ticketIdGenerator.MaxTicketId += 1; TableOperation replaceOperation = TableOperation.Replace(ticketIdGenerator); await this.CloudTable.ExecuteAsync(replaceOperation); return(ticketIdGenerator.MaxTicketId); } return(0); }