A custom Efficient Autonumber ID Generator Plugin in MS CRM

A custom plugin was written to create a sequential ID for an Entity without duplication with the use of locks in C# for MSCRM
Add this plugin in assembly registration tool and add the step in the Pre-Operation mode

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AutoNumberExample
{
    public class CaseAutonumber : IPlugin
    {
        private static readonly object SyncLock = new object();
        public void Execute(IServiceProvider serviceProvider)
        {

            IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            if (context.InputParameters.Contains("Target") &&
            context.InputParameters["Target"] is Entity)
            {
                if (context.IsInTransaction)
                {
                    int nextIncrementNumber = 0;
                    lock (SyncLock)
                    {
                        // Custom autonumber Entity having current sequence No of ID
                        string fetchXml = @"
                                             
                                             
                                             
                                             ";
                        EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchXml));
                        if (result.Entities.Count > 0)
                        {
                            Entity autonumber = result.Entities[0];
                            int counter = Convert.ToInt32(autonumber["ccs_casenumber"].ToString());
                            int newCounter = counter + 1;
                            autonumber.Attributes["ccs_casenumber"] = newCounter.ToString();
                            service.Update(autonumber);
                            nextIncrementNumber = newCounter;
                        }
                    }
                    //int counter = int.Parse(autonumber.Attributes["ccs_casenumber"].ToString(), CultureInfo.InvariantCulture);
                    if (nextIncrementNumber == 0)
                    {
                        throw new InvalidPluginExecutionException("Case number must have contain a value");
                    }
                    Entity entity = (Entity)context.InputParameters["Target"];
                    try
                    {
                        entity.Attributes["ccs_caseid"] = nextIncrementNumber;
                        entity.Attributes["ccs_casenumber"] = nextIncrementNumber.ToString();

                    }
                    catch (Exception ex)
                    {
                        throw new InvalidPluginExecutionException("An error occured in Autonumber Plugin: " + ex.Message.ToString(), ex);
                    }
                    finally
                    {

                    }

                }
            }
        }

    }
}