async Task<person> httpPaciente(string pacienteID, List<identifierDomain> dominios) { if (OPENEMPI_SESSION_KEY == null) getAuth(); using (var client = new HttpClient()) { client.BaseAddress = new Uri(OPENEMPI_URL); client.DefaultRequestHeaders.TryAddWithoutValidation("OPENEMPI_SESSION_KEY", OPENEMPI_SESSION_KEY); foreach (var dominio in dominios) { personIdentifier person = new personIdentifier { identifier = pacienteID, identifierDomain = new identifierDomain { namespaceIdentifier = dominio.namespaceIdentifier, universalIdentifier = dominio.universalIdentifier, universalIdentifierTypeCode = dominio.universalIdentifierTypeCode } }; string xml; XmlSerializer serializer = new XmlSerializer(typeof(personIdentifier)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); using (StringWriter sww = new StringWriter()) using (XmlWriter writer = XmlWriter.Create(sww, settings)) { serializer.Serialize(writer, person, ns); xml = sww.ToString(); } var httpContent = new StringContent(xml, Encoding.UTF8, "application/xml"); var response = await client.PostAsync("openempi-ws-rest/person-query-resource/findPersonById", httpContent); if (response.IsSuccessStatusCode) { } TextReader reader = new StringReader(response.Content.ReadAsStringAsync().Result); try { XmlSerializer personserializer = new XmlSerializer(typeof(person)); return (person)personserializer.Deserialize(reader); } catch (Exception e) { Console.WriteLine("Person not found at domain "+ dominio.identifierDomainId); } } } throw new Exception("OPENEMPI::Paciente NOT FOUND"); }
async Task<DataPaciente> parallelRequestPaciente(string pacienteID, List<identifierDomain> dominios) { if (OPENEMPI_SESSION_KEY == null) getAuth(); using (var client = new HttpClient()) { client.BaseAddress = new Uri(OPENEMPI_URL); client.DefaultRequestHeaders.TryAddWithoutValidation("OPENEMPI_SESSION_KEY", OPENEMPI_SESSION_KEY); List<StringContent> content = new List<StringContent>(); //serialize request FabricaSAREM f = new FabricaSAREM(); var schemas = f.adminController.getSchemas(); dominios = dominios.Where(x => schemas.Contains(x.namespaceIdentifier)).ToList(); foreach (var dominio in dominios) { Debug.WriteLine(dominio); if (schemas.Contains(dominio.namespaceIdentifier)) { personIdentifier person = new personIdentifier { identifier = pacienteID, identifierDomain = new identifierDomain { namespaceIdentifier = dominio.namespaceIdentifier, universalIdentifier = dominio.universalIdentifier, universalIdentifierTypeCode = dominio.universalIdentifierTypeCode } }; string xml; XmlSerializer serializer = new XmlSerializer(typeof(personIdentifier)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); using (StringWriter sww = new StringWriter()) using (XmlWriter writer = XmlWriter.Create(sww, settings)) { serializer.Serialize(writer, person, ns); xml = sww.ToString(); } var httpContent = new StringContent(xml, Encoding.UTF8, "application/xml"); content.Add(httpContent); } } //parallel! Debug.WriteLine("parallel!!!"); //var count = content.Count; List<Task<HttpResponseMessage>> tasklist = new List<Task<HttpResponseMessage>>(); foreach (var c in content) { tasklist.Add(client.PostAsync("openempi-ws-rest/person-query-resource/findPersonById", c)); } //finish all Debug.WriteLine("await2?"); Task.WhenAll(tasklist.ToArray()); int count = tasklist.Count; for (int i=0;i<count; i++) { Debug .WriteLine(tasklist[i].Result.Content.ReadAsStringAsync().Result); TextReader reader = new StringReader(tasklist[i].Result.Content.ReadAsStringAsync().Result); try { Debug.WriteLine(tasklist[i].Result.Content.ReadAsStringAsync().Result); XmlSerializer personserializer = new XmlSerializer(typeof(person)); person p = (person)personserializer.Deserialize(reader); string s = Sexo.UNKNOWN.ToString(); if (p.gender != null) { switch (p.gender.genderCode) { case "M": s = Sexo.MASCULINO.ToString(); break; case "F": s = Sexo.FEMENINO.ToString(); break; default: s = Sexo.UNKNOWN.ToString(); break; } } Paciente paciente = new Paciente { PacienteID= pacienteID, celular = p.cell, direccion = p.address1, FN = p.dateOfBirth, PaisID = p.countryCode, //nacion= new Pais{PaisID=p.countryCode, nombre= p.country}, nombre = p.middleName, mail = p.email, apellido = p.motherName, sexo = s, telefono = p.phoneNumber }; DataPaciente dp = new DataPaciente { paciente = paciente, mutualista = dominios[i].identifierDomainName }; return dp; } catch (Exception e) { Console.WriteLine("Person not found at domain "+ e.Message);//+ dominio.identifierDomainId); } } return null; } }