/**************
         * POST /api/Values HTTP/1.1
         * Host: localhost:55912
         * WorkItemType: Module.CommunityWatch.WorkItems.CommunityWatchWorkItem, Module.CommunityWatch, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
         * Content-Type: application/json
         * StateName: CWDetails
         * Cache-Control: no-cache
         * Postman-Token: 09de24be-217a-060c-650d-bc1f34ed9f3c
         *
         * {"states":{"CWDetails":[{"bookName":"ABC","bookAuthorName":"DEF"}],"CWDetailsDependant":[{"bookName":"GHI","bookAuthorName":"JKL"}]}}
         ****************/
        public override async Task <InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding effectiveEncoding)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (effectiveEncoding == null)
            {
                throw new ArgumentNullException(nameof(effectiveEncoding));
            }

            var request = context.HttpContext.Request;

            using (var reader = new StreamReader(request.Body, effectiveEncoding))
            {
                string clientStateJsonString = reader.ReadToEnd();
                var    objJSONEntity         = JsonConvert.DeserializeObject <JSONEntity>(clientStateJsonString);
                string detectedStateName     = objJSONEntity.StateName;
                try
                {
                    IWorkItem serverParentWorkItem  = context.HttpContext.RequestServices.GetService(typeof(IWorkItem)) as IWorkItem;
                    IWorkItem serverWorkItemInstace = serverParentWorkItem.WorkItems.Get(objJSONEntity.WorkItemId);
                    if (serverWorkItemInstace == null)
                    {
                        throw new Exception("Invalid WorkItem ID");
                    }
                    if (serverParentWorkItem != null)
                    {
                        serverWorkItemInstace.RegisterStateChangedEventForBroadcast();
                        var _entityTranslatorService = serverParentWorkItem.Container.Resolve <IEntityTranslatorService>();
                        JSONEntityDeSerializationParameters entityDeSerializationParameters = new JSONEntityDeSerializationParameters()
                        {
                            StateName = detectedStateName, WorkItem = serverWorkItemInstace
                        };
                        entityDeSerializationParameters.JSONObject = objJSONEntity.StateValue;
                        var deserializedStateObject = _entityTranslatorService.Translate <JSONEntityDeSerializationParameters>(entityDeSerializationParameters);
                        var taskList = serverParentWorkItem.Container.Resolve <IList <Task> >();
                        taskList.Clear();
                        serverWorkItemInstace.State[detectedStateName] = deserializedStateObject?.DeSerializedEntity;
                    }
                    return(await InputFormatterResult.SuccessAsync(JsonConvert.SerializeObject(serverWorkItemInstace.State[detectedStateName])));
                }
                catch
                {
                    return(await InputFormatterResult.FailureAsync());
                }
            }
        }
        public void Post([FromBody] dynamic value)
        {
            string clientStateJsonString = value.ToString();
            var    objJSONEntity         = JsonConvert.DeserializeObject <JSONEntity>(clientStateJsonString);
            string detectedStateName     = objJSONEntity.StateName;

            try
            {
                IWorkItem serverParentWorkItem  = _workItem;
                IWorkItem serverWorkItemInstace = serverParentWorkItem.WorkItems.Get(objJSONEntity.WorkItemId);
                if (serverWorkItemInstace == null)
                {
                    throw new Exception("Invalid WorkItem ID");
                }
                if (serverParentWorkItem != null)
                {
                    serverWorkItemInstace.RegisterStateChangedEventForBroadcast();
                    var _entityTranslatorService = serverParentWorkItem.Container.Resolve <IEntityTranslatorService>();
                    JSONEntityDeSerializationParameters entityDeSerializationParameters = new JSONEntityDeSerializationParameters()
                    {
                        StateName = detectedStateName, WorkItem = serverWorkItemInstace
                    };
                    entityDeSerializationParameters.JSONObject = objJSONEntity.StateValue;
                    var deserializedStateObject = _entityTranslatorService.Translate <JSONEntityDeSerializationParameters>(entityDeSerializationParameters);
                    var tasks = serverParentWorkItem.Container.Resolve <IList <Task> >();
                    tasks.Clear();
                    serverWorkItemInstace.State[detectedStateName] = deserializedStateObject?.DeSerializedEntity;
                }
            }
            catch
            {
            }

            var taskList = _workItem.Container.Resolve <IList <Task> >();

            Task.WaitAll(taskList.Where(e => e != null).ToArray());
            //await Task.Run(() => { Task.WaitAll(taskList.Where(e => e != null).ToArray()); });
            //await Task.Run(()=> { Thread.CurrentThread.Join(); });
            //TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
            //await tcs.Task.ContinueWith((obj) => { }, TaskContinuationOptions.OnlyOnRanToCompletion);
        }