public static Task <ResultEx <TResult> > ThenTryAsync <TValue, TError, TResult>(this ResultGeneric <TValue, TError> result, Func <TValue, Task <ResultGeneric <TResult, TError> > > func) { if (result.IsError) { return(Task.FromResult(ResultEx <TResult> .FromError(ResultErrorException.FromError(result.Error)))); } try { return(func(result.Value).ContinueWith(task => task.Result.ToResultEx(), TaskContinuationOptions.DenyChildAttach | TaskContinuationOptions.ExecuteSynchronously)); } catch (Exception e) { return(Task.FromResult(ResultEx <TResult> .FromError(e))); } }
public static Task <ResultGeneric <TResult, TError> > ThenTryAsync <TValue, TError, TResult>(this ResultGeneric <TValue, TError> result, Func <TValue, Task <ResultGeneric <TResult, TError> > > func, Func <Exception, TError> catchFunc) { if (result.IsError) { return(Task.FromResult(ResultGeneric <TResult, TError> .FromError(result.Error))); } try { return(func(result.Value)); } catch (Exception e) { return(Task.FromResult(ResultGeneric <TResult, TError> .FromError(catchFunc(e)))); } }
public static Task <ResultGeneric <TValue, TError> > OrAsync <TValue, TError>(this Task <ResultGeneric <TValue, TError> > resultTask, ResultGeneric <TValue, TError> elseResult) { return(resultTask.ContinueWith(task => { var result = task.Result; return result.IsError ? elseResult : result; }, TaskContinuationOptions.ExecuteSynchronously)); }
public static Task <ResultGeneric <TResult, TError> > ThenTryAsync <TValue, TError, TResult>(this ResultGeneric <TValue, TError> result, Func <TValue, Task <TResult> > func, Func <Exception, TError> catchFunc) { if (result.IsError) { return(Task.FromResult(ResultGeneric <TResult, TError> .FromError(result.Error))); } try { return(func(result.Value).ContinueWith(task => task.IsFaulted ? ResultGeneric <TResult, TError> .FromError(catchFunc(task.Exception.TryFlattenAggregateException())) : ResultGeneric <TResult, TError> .FromValue(task.Result), CancellationToken.None, TaskContinuationOptions.DenyChildAttach | TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default)); } catch (Exception e) { return(Task.FromResult(ResultGeneric <TResult, TError> .FromError(catchFunc(e)))); } }
public static ResultHasNoErrorException From <TValue, TError>(ResultGeneric <TValue, TError> _) => new ResultHasNoErrorException(string.Format(MESSAGE, typeof(ResultGeneric <TValue, TError>).Name));
public static ResultGeneric BatchUpdate(string logJobName, Batch batch) { ResultGeneric result = new ResultGeneric(); result.ReturnCode = 0; result.Message = ""; result.RecordsCount = 0; try { lock (lockObj) { LogManager.Configuration.Variables["JobName"] = logJobName; HttpClient client = new HttpClient(); client.Timeout = TimeSpan.FromMinutes(15); string URL = ""; string bodyString = ""; string returnMessage = ""; string batchJS = ""; batchJS = JsonConvert.SerializeObject(batch, Newtonsoft.Json.Formatting.Indented); batchJS = batchJS.Replace(@"\", "\\\\"); URL = BatchDeliveryService.BaseURL + "Batches/UpdateBatchInformation"; bodyString = "'" + batchJS + "'"; HttpContent body_for_new = new StringContent(bodyString); body_for_new.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpResponseMessage response_for_new = client.PostAsync(URL, body_for_new).Result; if (!response_for_new.IsSuccessStatusCode) { nlogger.Error("Error:" + "\r\n" + response_for_new.ReasonPhrase + "\r\n" + response_for_new.RequestMessage); Console.WriteLine("Error:" + "\r\n" + response_for_new.ReasonPhrase + "\r\n" + response_for_new.RequestMessage); result.Message = "Error:" + "\r\n" + response_for_new.ReasonPhrase + "\r\n" + response_for_new.RequestMessage; result.ReturnCode = -1; } else { using (HttpContent content = response_for_new.Content) { Task <string> resultTemp = content.ReadAsStringAsync(); returnMessage = resultTemp.Result; result = JsonConvert.DeserializeObject <ResultGeneric>(returnMessage); } } } } catch (Exception ex) { lock (lockObj) { LogManager.Configuration.Variables["JobName"] = logJobName; nlogger.Fatal(General.ErrorMessage(ex)); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(General.ErrorMessage(ex)); Console.ForegroundColor = ConsoleColor.White; result.Message = General.ErrorMessage(ex); result.ReturnCode = -2; } } return(result); }
public static ResultGeneric BatchTrackingEvent(string logJobName, string batchNumber, string currentStatus) { ResultGeneric result = new ResultGeneric(); result.ReturnCode = 0; result.Message = ""; result.RecordsCount = 0; try { lock (lockObj) { //batchNumber = Uri.EscapeDataString(batchNumber); LogManager.Configuration.Variables["JobName"] = logJobName; BatchTracking batchTracking = new BatchTracking(); HttpClient client = new HttpClient(); client.Timeout = TimeSpan.FromMinutes(15); string URL = ""; string bodyString = ""; string returnMessage = ""; string batchTrackingJS = ""; batchTracking.BatchNumber = batchNumber; batchTracking.InitialStatus = currentStatus; batchTracking.FinalStatus = "Waiting for Indexing"; batchTracking.Event = "Batch Delivered to Resting Location"; batchTracking.OperatorName = "Batch Delivery Service"; batchTracking.StationName = Dns.GetHostName(); batchTrackingJS = JsonConvert.SerializeObject(batchTracking, Newtonsoft.Json.Formatting.Indented); batchTrackingJS = batchTrackingJS.Replace(@"\", "\\\\"); URL = BatchDeliveryService.BaseURL + "Batches/NewBatchEvent"; bodyString = "'" + batchTrackingJS + "'"; HttpContent body_for_new = new StringContent(bodyString); body_for_new.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpResponseMessage response_for_new = client.PostAsync(URL, body_for_new).Result; if (!response_for_new.IsSuccessStatusCode) { nlogger.Error("Error:" + "\r\n" + response_for_new.ReasonPhrase + "\r\n" + response_for_new.RequestMessage); Console.WriteLine("Error:" + "\r\n" + response_for_new.ReasonPhrase + "\r\n" + response_for_new.RequestMessage); result.Message = "Error:" + "\r\n" + response_for_new.ReasonPhrase + "\r\n" + response_for_new.RequestMessage; result.ReturnCode = -1; } else { using (HttpContent content = response_for_new.Content) { Task <string> resultTemp = content.ReadAsStringAsync(); returnMessage = resultTemp.Result; result = JsonConvert.DeserializeObject <ResultGeneric>(returnMessage); } } } } catch (Exception ex) { lock (lockObj) { LogManager.Configuration.Variables["JobName"] = logJobName; nlogger.Fatal(General.ErrorMessage(ex)); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(General.ErrorMessage(ex)); Console.ForegroundColor = ConsoleColor.White; result.Message = General.ErrorMessage(ex); result.ReturnCode = -2; } } return(result); }
public static ResultGeneric BatchRegistration(string logJobName, string batchNumber, string batchAlias, JobExtended job) { ResultGeneric result = new ResultGeneric(); result.ReturnCode = 0; result.Message = ""; result.RecordsCount = 0; try { lock (lockObj) { LogManager.Configuration.Variables["JobName"] = logJobName; Batch batch = new Batch(); HttpClient client = new HttpClient(); client.Timeout = TimeSpan.FromMinutes(15); string URL = ""; string bodyString = ""; string returnMessage = ""; string batchJS = ""; batch.BatchNumber = batchNumber; batch.StatusFlag = "Ready to Scan"; batch.SubmittedBy = "Auto Import Service"; batch.SubmittedDate = DateTime.Now; batch.Customer = job.CustomerName; batch.ProjectName = job.ProjectName; batch.JobType = job.JobName; batch.DepName = job.DepartmentName; batch.BatchAlias = batchAlias; batchJS = JsonConvert.SerializeObject(batch, Newtonsoft.Json.Formatting.Indented); batchJS = batchJS.Replace(@"\", "\\\\"); URL = BatchDeliveryService.BaseURL + "Batches/BatchRegistration"; bodyString = "'" + batchJS + "'"; HttpContent body_for_new = new StringContent(bodyString); body_for_new.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpResponseMessage response_for_new = client.PostAsync(URL, body_for_new).Result; if (!response_for_new.IsSuccessStatusCode) { nlogger.Error("Error:" + "\r\n" + response_for_new.ReasonPhrase + "\r\n" + response_for_new.RequestMessage); Console.WriteLine("Error:" + "\r\n" + response_for_new.ReasonPhrase + "\r\n" + response_for_new.RequestMessage); result.Message = "Error:" + "\r\n" + response_for_new.ReasonPhrase + "\r\n" + response_for_new.RequestMessage; result.ReturnCode = -1; } else { using (HttpContent content = response_for_new.Content) { Task <string> resultTemp = content.ReadAsStringAsync(); returnMessage = resultTemp.Result; result = JsonConvert.DeserializeObject <ResultGeneric>(returnMessage); } } } } catch (Exception ex) { lock (lockObj) { LogManager.Configuration.Variables["JobName"] = logJobName; nlogger.Fatal(General.ErrorMessage(ex)); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(General.ErrorMessage(ex)); Console.ForegroundColor = ConsoleColor.White; result.Message = General.ErrorMessage(ex); result.ReturnCode = -2; } } return(result); }
/// <summary> /// Add a new Document for a given Batch /// </summary> /// <param name="logJobName"></param> /// <param name="batchNumber"></param> /// <param name="batchDoc"></param> /// <returns></returns> public static ResultGeneric NewBatchDocument(string logJobName, string batchNumber, BatchDocs batchDoc) { ResultGeneric result = new ResultGeneric(); result.ReturnCode = 0; result.Message = ""; result.RecordsCount = 0; try { lock (lockObj) { HttpClient client = new HttpClient(); client.Timeout = TimeSpan.FromMinutes(15); string URL = ""; string bodyString = ""; string returnMessage = ""; string batcDocJS = ""; // StringEscapeHandling help to scape Single Quote in Jason string Format batcDocJS = JsonConvert.SerializeObject(batchDoc, Newtonsoft.Json.Formatting.Indented, new Newtonsoft.Json.JsonSerializerSettings { StringEscapeHandling = Newtonsoft.Json.StringEscapeHandling.EscapeHtml }); batcDocJS = batcDocJS.Replace(@"\", "\\\\"); URL = BatchRemoverService.BaseURL + "Batches/NewBatchDocument"; bodyString = "'" + batcDocJS + "'"; HttpContent body_for_new = new StringContent(bodyString); body_for_new.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpResponseMessage response_for_new = client.PostAsync(URL, body_for_new).Result; if (!response_for_new.IsSuccessStatusCode) { General.Logger(General.LogLevel.Error, "Error:" + "\r\n" + response_for_new.ReasonPhrase + "\r\n" + response_for_new.RequestMessage, logJobName); Console.WriteLine("Error:" + "\r\n" + response_for_new.ReasonPhrase + "\r\n" + response_for_new.RequestMessage); result.Message = "Error:" + "\r\n" + response_for_new.ReasonPhrase + "\r\n" + response_for_new.RequestMessage; result.ReturnCode = -1; } else { using (HttpContent content = response_for_new.Content) { Task <string> resultTemp = content.ReadAsStringAsync(); returnMessage = resultTemp.Result; result = JsonConvert.DeserializeObject <ResultGeneric>(returnMessage); } } } } catch (Exception ex) { lock (lockObj) { General.Logger(General.LogLevel.Error, General.ErrorMessage(ex), logJobName); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(General.ErrorMessage(ex)); Console.ForegroundColor = ConsoleColor.White; result.Message = General.ErrorMessage(ex); result.ReturnCode = -2; } } return(result); }