public static R InvocarServicio <R>(Object objRequest, string url) where R : new() { ManejadorLogEventos manejadorLogEventos = new ManejadorLogEventos(); R response = new R(); string responseFromServer = string.Empty; string json = string.Empty; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json"; request.Accept = "application/json"; json = Utilitario.SerializarObjectInJson(objRequest); var dataStream = new StreamWriter(request.GetRequestStream()); dataStream.Write(json); dataStream.Close(); var webResponse = (HttpWebResponse)request.GetResponse(); var streamReader = new StreamReader(webResponse.GetResponseStream()); responseFromServer = streamReader.ReadToEnd(); response = Utilitario.JsonDeserialize <R>(responseFromServer); manejadorLogEventos.Break(); manejadorLogEventos.GrabarLogMapaBD(manejadorLogEventos.inicio, manejadorLogEventos.fin, manejadorLogEventos.GetDuracion(), (manejadorLogEventos.GetDuracion() / 1000), Helper.LocalIPAddress(), "SwCarCool", "Proxy", url, json, responseFromServer, "LOG_MAPA_SERVICIO", null, null, null, null); } catch (Exception ex) { InsertLog.Instanse.Insert(string.Format(@"Error en el metodo: {0}{1}Servicio Web: {2}{3}Trama Request: {4}{5}Trama Response: {6}{7}Detalle Error: {8}{9}Mensaje Error: {10}", MethodBase.GetCurrentMethod().Name, Environment.NewLine, url, Environment.NewLine, json, Environment.NewLine, responseFromServer, Environment.NewLine, ex.StackTrace, Environment.NewLine, ex.Message)); manejadorLogEventos.Break(); manejadorLogEventos.GrabarLogMapaBD(manejadorLogEventos.inicio, manejadorLogEventos.fin, manejadorLogEventos.GetDuracion(), (manejadorLogEventos.GetDuracion() / 1000), Helper.LocalIPAddress(), "SwCarCool", "Proxy", url, json, responseFromServer, "LOG_MAPA_SERVICIO", null, null, null, null); } return(response); }
// This method is executed at runtime inside your application, // when target methods exit with success. public override void OnSuccess(MethodExecutionArgs args) { try { string tramaOutput = ""; manejadorLogEventos.Break(); try { tramaOutput = Helper.SerializarJsonObjecto(args.ReturnValue); } catch (Exception ex) { tramaOutput = "ERROR AL SERIALIZAR JSON"; ManejadorExcepciones.PublicarExcepcion(ex, PoliticaExcepcion.ServicioWCF); } string ip = String.Empty; var props = OperationContext.Current.IncomingMessageProperties; var endpointProperty = props[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; if (endpointProperty != null) { ip = endpointProperty.Address; } var flagTrazasServicioWCF = ConfigurationManager.AppSettings["FlagTrazasServicioWCF"]; if (flagTrazasServicioWCF == "S") { try { // var objetoTrazabilidad = Helper.ConvertirAObjeto<ObjetoTrazabilidad>("ObjetoTrazabilidad", tramaInput); manejadorLogEventos.GrabarLogMapaBD(manejadorLogEventos.inicio, manejadorLogEventos.fin, manejadorLogEventos.GetDuracion(), (manejadorLogEventos.GetDuracion() / 1000), ip, nombreUsuario, "WCF", url, tramaInput, tramaOutput, "LOG_MAPA_SERVICIO", null, null, null, null); // manejadorLogEventos.GrabarLogMapaBD(manejadorLogEventos.inicio, manejadorLogEventos.fin, manejadorLogEventos.GetDuracion(), (manejadorLogEventos.GetDuracion() / 1000), ip, nombreUsuario, "WCF", url, tramaInput, tramaOutput, "LOG_MAPA_SERVICIO", null, objetoTrazabilidad != null ? objetoTrazabilidad.GuidFormulario : null, objetoTrazabilidad != null ? objetoTrazabilidad.ValorReferencial : null, objetoTrazabilidad != null ? objetoTrazabilidad.GuidEvento : null); } catch (Exception) { manejadorLogEventos.GrabarLogMapa(manejadorLogEventos.inicio.ToString(), manejadorLogEventos.fin.ToString(), manejadorLogEventos.GetDuracion().ToString(), (manejadorLogEventos.GetDuracion() / 1000).ToString(), ip, nombreUsuario, "WCF", url, tramaInput, tramaOutput); } // } } catch (Exception ex) { ManejadorExcepciones.PublicarExcepcion(ex, PoliticaExcepcion.ServicioWCF); } }
public Y DeserializarJSON <T, Y>(T request, string url, string GuidProceso = null, string GuidEvento = null, string soapAction = "", bool consultaSap = false) { object vnull = null; Y resultServicio = (Y)(vnull); ManejadorLogEventos manejadorLogEventosMAPA = new ManejadorLogEventos(); ManejadorLogEventos manejadorLogEventosLOGERRORES = new ManejadorLogEventos(); String Request = ""; String Response = ""; var flagTrazasServicioWCF = ConfigurationManager.AppSettings["FlagTrazasServicioWCF"]; var flagTrazasServicioBrocker = ConfigurationManager.AppSettings["FlagTrazasServicioBrocker"]; try { var RESTProxy = new MyWebClient(); if (!string.IsNullOrEmpty(soapAction)) { RESTProxy.Headers["SOAPAction"] = soapAction; } RESTProxy.Headers["Content-type"] = "application/json"; MemoryStream ms = new MemoryStream(); Stream stream = ms; bool SeEjecutadeWeb = true; bool conrequest = true; if (request.ToString() == string.Empty) { try { stream = RESTProxy.OpenRead(url); SeEjecutadeWeb = true; } catch { SeEjecutadeWeb = false; ms = new MemoryStream(new UTF8Encoding().GetBytes("")); } conrequest = false; } else { if (consultaSap) { Request = JsonConvert.SerializeObject(request, Newtonsoft.Json.Formatting.None, new IsoDateTimeConverter() { DateTimeFormat = "dd.MM.yyyy" }); // HH:mm:ss ms = new MemoryStream(new UTF8Encoding().GetBytes(Request)); ms.Position = 0; } else { var serializerToUpload = new DataContractJsonSerializer(typeof(T)); serializerToUpload.WriteObject(ms, request); ms.Position = 0; Request = Encoding.UTF8.GetString(ms.ToArray()); } if (consultaSap && flagTrazasServicioWCF == "S") { manejadorLogEventosLOGERRORES.GuardarTrama2(manejadorLogEventosLOGERRORES.inicio, "REQUEST", url, Helper.LocalIPAddress(), Environment.UserName, Request); } } // if (SeEjecutadeWeb == false || conrequest == true) { System.Net.ServicePointManager.Expect100Continue = false; var dc = System.Text.Encoding.UTF8.GetString(RESTProxy.UploadData(url, "POST", ms.ToArray())).ToCharArray(); var data = System.Text.Encoding.UTF8.GetBytes(dc); Stream stream_response; stream_response = new MemoryStream(data); stream_response.Position = 0; var sr_response = new StreamReader(stream_response); sr_response.ReadToEnd(); Response = Encoding.UTF8.GetString(data.ToArray()); if (consultaSap && flagTrazasServicioBrocker == "S") { manejadorLogEventosLOGERRORES.Break(); manejadorLogEventosLOGERRORES.GuardarTrama2(manejadorLogEventosLOGERRORES.fin, "RESPONSE", url, Helper.LocalIPAddress(), Environment.UserName, Response); } stream = new MemoryStream(data); } if (consultaSap) { StreamReader sReader = new StreamReader(stream); Response = sReader.ReadToEnd(); resultServicio = (Y)JsonConvert.DeserializeObject(Response, typeof(Y), new JsonSerializerSettings() { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore }); manejadorLogEventosMAPA.Break(); if (flagTrazasServicioBrocker == "S") { manejadorLogEventosMAPA.GrabarLogMapaBD(manejadorLogEventosMAPA.inicio, manejadorLogEventosMAPA.fin, manejadorLogEventosMAPA.GetDuracion(), (manejadorLogEventosMAPA.GetDuracion() / 1000), Helper.LocalIPAddress(), "", "BROKER", url, Request, Response, "LOG_MAPA_BROKER", null, GuidProceso, null, GuidEvento); } } else { var obj = new DataContractJsonSerializer(typeof(Y)); resultServicio = (Y)obj.ReadObject(stream); manejadorLogEventosMAPA.Break(); if (flagTrazasServicioWCF == "S") { manejadorLogEventosMAPA.GrabarLogMapaBD(manejadorLogEventosMAPA.inicio, manejadorLogEventosMAPA.fin, manejadorLogEventosMAPA.GetDuracion(), (manejadorLogEventosMAPA.GetDuracion() / 1000), Helper.LocalIPAddress(), "", "PROXY", url, Request, Response, "LOG_MAPA_WCF", null, GuidProceso, null, GuidEvento); } } } catch (Exception ex) { ManejadorExcepciones.PublicarExcepcion(ex, PoliticaExcepcion.Framework); if (flagTrazasServicioBrocker == "S") { manejadorLogEventosMAPA.Break(); manejadorLogEventosMAPA.GrabarLogMapaBD(manejadorLogEventosMAPA.inicio, manejadorLogEventosMAPA.fin, manejadorLogEventosMAPA.GetDuracion(), (manejadorLogEventosMAPA.GetDuracion() / 1000), Helper.LocalIPAddress(), "", "PROXY", url, Request, ex.Message, "LOG_MAPA_WCF", null, GuidProceso, null, GuidEvento); } } return(resultServicio); }