public static async Task <BatchScoreStatus> InvokeBatchExecutionService(BES_Obj besobj)
        {
            // set a time out for polling status
            //const int TimeOutInMilliseconds = 120 * 10000; // Set a timeout of 20 minutes
            BatchScoreStatus status  = new BatchScoreStatus();
            string           BaseUrl = besobj.GetAPIURL();

            string strApiVersion = besobj.GetApiVersion();

            if (!strApiVersion.Contains("api-version=2.0"))
            {
                status.StatusCode = 0;
                status.Details    = "Invalid API version. Please check your Post URL is from version 2.0 of the API help page.";
                status.SetAdditionInformation();
                return(status);
            }

            BaseUrl = BaseUrl.Substring(0, BaseUrl.LastIndexOf("/jobs") + 5);   // Correct BaseUrl don't have api-version
            string apiKey = besobj.GetAPIKey();


            using (HttpClient client = new HttpClient())
            {
                var request = new BatchExecutionRequest()
                {
                    Input            = besobj.GenerateInput(),
                    Outputs          = besobj.GenerateOutputs(),
                    GlobalParameters = besobj.GenerateGlobalParameters()
                };

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);


                //Submitting the job...
                // submit the job
                var response = await client.PostAsJsonAsync(BaseUrl + "?" + strApiVersion, request);

                if (!response.IsSuccessStatusCode)
                {
                    status.StatusCode = 0;
                    status.Details    = response.ReasonPhrase + ". Job submission encountered error. Please check input.";
                    status.SetAdditionInformation();
                    return(status);
                }

                string jobId = await response.Content.ReadAsAsync <string>();

                Console.WriteLine(string.Format("Job ID: {0}", jobId));

                response = await client.PostAsync(BaseUrl + "/" + jobId + "/start?" + strApiVersion, null);

                string jobLocation = BaseUrl + "/" + jobId + "?" + strApiVersion;
                status.JobLocation = jobLocation;
                return(status);


                // start the job
                //Starting the job...
                //response = await client.PostAsync(BaseUrl + "/" + jobId + "/start?" + strApiVersion, null);
                //if (!response.IsSuccessStatusCode)
                //{
                //    status.StatusCode = 0;
                //    status.Details = response.ReasonPhrase + ". Cannot get status of Job Id " + jobId;
                //    status.SetAdditionInformation();
                //    return null;
                //}

                //string jobLocation = BaseUrl + "/" + jobId + "?" + strApiVersion;
                //Stopwatch watch = Stopwatch.StartNew();
                //bool done = false;
                //while (!done)

                //{
                //    //Checking the job status...
                //    response = await client.GetAsync(jobLocation);
                //    if (!response.IsSuccessStatusCode)
                //    {

                //        return null;
                //    }

                //    status = await response.Content.ReadAsAsync<BatchScoreStatus>();
                //    if (watch.ElapsedMilliseconds > TimeOutInMilliseconds)
                //    {
                //        done = true;
                //        await client.DeleteAsync(jobLocation);
                //    }
                //    switch (status.StatusCode)
                //    {
                //        case BatchScoreStatusCode.NotStarted:
                //            break;
                //        case BatchScoreStatusCode.Running:
                //            break;
                //        case BatchScoreStatusCode.Failed:
                //            done = true;
                //            break;
                //        case BatchScoreStatusCode.Cancelled:
                //            done = true;
                //            break;
                //        case BatchScoreStatusCode.Finished:
                //            done = true;
                //            break;
                //    }

                //    if (!done)
                //    {
                //        Thread.Sleep(1000); // Wait one second
                //    }
                //}

                //status.SetAdditionInformation();
                //return status;
            }
        }
        public static async Task<BatchScoreStatus> InvokeBatchExecutionService(BES_Obj besobj)
        {
            

            // set a time out for polling status
            //const int TimeOutInMilliseconds = 120 * 10000; // Set a timeout of 20 minutes
            BatchScoreStatus status = new BatchScoreStatus();
            string BaseUrl = besobj.GetAPIURL();

            string strApiVersion = besobj.GetApiVersion();
            if(!strApiVersion.Contains("api-version=2.0"))
            {
                status.StatusCode = 0;
                status.Details = "Invalid API version. Please check your Post URL is from version 2.0 of the API help page.";
                status.SetAdditionInformation();
                return status;
            }

            BaseUrl = BaseUrl.Substring(0, BaseUrl.LastIndexOf("/jobs") + 5);   // Correct BaseUrl don't have api-version
            string apiKey = besobj.GetAPIKey();

            
            using (HttpClient client = new HttpClient())
            {
                var request = new BatchExecutionRequest()
                {
                    Input = besobj.GenerateInput(),
                    Outputs = besobj.GenerateOutputs(),
                    GlobalParameters = besobj.GenerateGlobalParameters()                    
                };

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);


                //Submitting the job...
                // submit the job
                var response = await client.PostAsJsonAsync(BaseUrl + "?" + strApiVersion, request);
                if (!response.IsSuccessStatusCode)
                {
                    status.StatusCode = 0;
                    status.Details = response.ReasonPhrase + ". Job submission encountered error. Please check input.";
                    status.SetAdditionInformation();
                    return status;
                }

                string jobId = await response.Content.ReadAsAsync<string>();
                Console.WriteLine(string.Format("Job ID: {0}", jobId));

                response = await client.PostAsync(BaseUrl + "/" + jobId + "/start?" + strApiVersion, null);

                string jobLocation = BaseUrl + "/" + jobId + "?" + strApiVersion;
                status.JobLocation = jobLocation;
                return status;


                // start the job
                //Starting the job...
                //response = await client.PostAsync(BaseUrl + "/" + jobId + "/start?" + strApiVersion, null);
                //if (!response.IsSuccessStatusCode)
                //{
                //    status.StatusCode = 0;
                //    status.Details = response.ReasonPhrase + ". Cannot get status of Job Id " + jobId;
                //    status.SetAdditionInformation();
                //    return null;
                //}

                //string jobLocation = BaseUrl + "/" + jobId + "?" + strApiVersion;
                //Stopwatch watch = Stopwatch.StartNew();
                //bool done = false;                
                //while (!done)

                //{
                //    //Checking the job status...
                //    response = await client.GetAsync(jobLocation);
                //    if (!response.IsSuccessStatusCode)
                //    {   
                        
                //        return null;
                //    }

                //    status = await response.Content.ReadAsAsync<BatchScoreStatus>();
                //    if (watch.ElapsedMilliseconds > TimeOutInMilliseconds)
                //    {
                //        done = true;
                //        await client.DeleteAsync(jobLocation);
                //    }
                //    switch (status.StatusCode)
                //    {
                //        case BatchScoreStatusCode.NotStarted:
                //            break;
                //        case BatchScoreStatusCode.Running:
                //            break;
                //        case BatchScoreStatusCode.Failed:
                //            done = true;
                //            break;
                //        case BatchScoreStatusCode.Cancelled:
                //            done = true;
                //            break;
                //        case BatchScoreStatusCode.Finished:
                //            done = true;
                //            break;
                //    }

                //    if (!done)
                //    {
                //        Thread.Sleep(1000); // Wait one second
                //    }
                //}

                //status.SetAdditionInformation();
                //return status;
            }

        }