/// <summary> /// Save information about files rejected by the Middleware app. /// information include the Json frame sent to MW. /// </summary> /// <param name="prob"></param> /// <returns></returns> public int writeProblemFile(ProblemFiles prob) { int resp = 0; try { System.Collections.Specialized.NameValueCollection args = new NameValueCollection(); args.Add("@fileType", prob.FileType.ToString()); args.Add("@fileName", prob.FileName.ToString()); args.Add("@fileNumber", prob.FileNumber.ToString()); args.Add("@jsonData", prob.JsonData.ToString()); DBContext.DBAccess access = new DBContext.DBAccess(); // ===== > resp = access.ExecuteNonQuery("[MW_NavImportProblemsInsert]", DBContext.DBAccess.DBConnection.SqlMainNew, args); // < ===== } catch { writeSyncLog(1, Convert.ToInt16(ConfigurationManager.AppSettings["ServiceID"]), 1, ConfigurationManager.AppSettings["TableName"], "Service for " + ConfigurationManager.AppSettings["TableName"] + " MW_NavImportProblems access error "); } finally { } return(resp); }
/// *************************************************************************************** /// <summary> /// Version 10/15/2015 - 14:48 /// Consume web services, in case of error, an error description entry is included /// in the log file. /// WeServiceTimer (method performance measure) and WebServiceOption (Production /// or test) options are controlled through AppConfig.File. /// </summary> /// <param name="frame">Json Data string</param> /// <param name="fName">Processed File name</param> /// <param name="fType">File type (Raw Data or File data) </param> unused /// <param name="fData">WebService url </param> /// <returns>Bool value to true if procces was OK</returns> /// -------------------------------------------------------------------------------------- public ServiceResponse ConsumeWebService(string frame, string fName, string fData, EtlTimer syncRow) //string fType, string fData) { /// Process measurement option, only apply when WebServicesTimer has been set to Yes /// in the AppConfig.file Will measure the process time from starting this method, until /// the response from the WebService is received. Stopwatch stopWatch = new Stopwatch(); if ((ConfigurationManager.AppSettings["WebServicesTimer"].ToString()) == "Yes") { stopWatch.Start(); } /// ---------------------------------------------------------------------------------- //WriteLogFile wlf = new WriteLogFile(); //WriteErrorFile wef = new WriteErrorFile(); ServiceResponse err = new ServiceResponse(); /// process errors description object ServiceResponse resp = new ServiceResponse(); /// Response from web service /// err.FileName = fName; err.Request = frame; /// //bool res = true; var result = string.Empty; try { /// ****************************************************************************** /// Call Web Service /// Web service return string message with operation result. /// Access to Middleware API is controlled through etlTimer Table (IsApiRunning) /// ------------------------------------------------------------------------------ if (syncRow.IsApiRunning) { string uri = fData; var request = (HttpWebRequest)WebRequest.Create(uri); request.ContentType = "text/json"; request.Method = "POST"; using (var client = new WebClient()) { /// Send json data to api supporting special characters within the json data client.Headers[HttpRequestHeader.ContentType] = "application/json"; // Optionally specify an encoding for uploading and downloading strings. client.Encoding = System.Text.Encoding.UTF8; // Upload the data. result = client.UploadString(uri, "POST", frame); //resp.Status = "Error"; // force error response only for testing purposes } resp = JsonConvert.DeserializeObject <ServiceResponse>(result); // ----------------------------------------------------------------------------------------- // When api service return an error response (status not 'OK'), // save the data file information in MW_NavImportProblems table // ----------------------------------------------------------------------------------------- if (resp.Status != "OK") { resp.IsOk = false; // Set response flag to no ok int position = fName.LastIndexOf('-'); ProblemFiles prob = new ProblemFiles(); prob.FileName = fName; if (position == -1) { prob.FileNumber = "n/a"; } else { prob.FileNumber = fName.Substring(position + 1); } prob.JsonData = frame; prob.FileType = ConfigurationManager.AppSettings["FileType"]; // ================ > int write = wtf.writeProblemFile(prob); // < ================ } } else { /// ---------------------------------------------------------------------------- /// Testing webService (using proxy) option, apply only when WebServices Option /// is not set to IsApiRunning in the etlTimer table. /// To use the ApiTest option, uncomment the following code /// ---------------------------------------------------------------------------- //ServiceOrdersClient proxy = new ServiceOrdersClient(); //// route webservice based on file type //result = proxy.AddOrder(frame, fName, fType); resp.Status = "OK"; resp.IsOk = true; } /// -------------------------------------------------------------------------------- /// /// -------------------------------------------------------------------------------- if (!String.IsNullOrEmpty(resp.Status) && (resp.Status != "OK")) { // res = false; resp.IsOk = false; err.NISOrderId = resp.NISOrderId; err.Status = resp.Status; err.Message = "Order id.: " + resp.NISOrderId + " Message: " + resp.Message; /// WriteErrorFile(err, syncRow); } } catch (Exception ex) { int position = fName.LastIndexOf('-'); ProblemFiles prob = new ProblemFiles(); prob.FileName = fName; if (position == -1) { prob.FileNumber = "n/a"; } else { prob.FileNumber = fName.Substring(position + 1); } prob.JsonData = frame; prob.FileType = ConfigurationManager.AppSettings["FileType"]; // ================ > int write = wtf.writeProblemFile(prob); // < ================ /// Replace service log error by WebService call err.NISOrderId = resp.NISOrderId; err.Status = "Not processed"; err.Message = "An error occured while running " + fData + " Web Service, : " + ex.Message; /// WriteErrorFile(err, syncRow); // res = false; resp.IsOk = false; } if ((ConfigurationManager.AppSettings["WebServicesTimer"].ToString()) == "Yes") { stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // Format and display the TimeSpan value. string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("File: " + fName + " WebService RunTime " + elapsedTime); } if (resp.Status == "OK") { resp.IsOk = true; } return(resp); }