From bb59b026187375eff651aecbe227f1f38e02fa59 Mon Sep 17 00:00:00 2001 From: williamoura <32176212+williamoura@users.noreply.github.com> Date: Thu, 11 Nov 2021 09:24:26 -0300 Subject: [PATCH] Ajustes para melhor funcionamento do Validador (MDFe) e do SoapUtils (#1304) Co-authored-by: William de Quadros Moura --- Shared.DFe.Wsdl/Common/SoapUtils.cs | 24 ++++++++++++------------ Shared.MDFe.Utils/Validacao/Validador.cs | 4 +++- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/Shared.DFe.Wsdl/Common/SoapUtils.cs b/Shared.DFe.Wsdl/Common/SoapUtils.cs index b9975005a..bea03dc60 100644 --- a/Shared.DFe.Wsdl/Common/SoapUtils.cs +++ b/Shared.DFe.Wsdl/Common/SoapUtils.cs @@ -89,12 +89,12 @@ public async Task SendRequestAsync(XmlDocument xmlEnvelop, X509Certifica streamWriter.Write(xmlSoap, 0, Encoding.UTF8.GetBytes(xmlSoap).Length); streamWriter.Close(); - var webResponse = httpWr.GetResponse(); - var respStream = webResponse.GetResponseStream(); - StreamReader streamReader = new StreamReader(respStream); - - string xmlRetorno = streamReader.ReadToEnd(); - return await Task.FromResult(xmlRetorno); + using (HttpWebResponse httpResponse = (HttpWebResponse)httpWr.GetResponse()) + using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) + { + string xmlRetorno = streamReader.ReadToEnd(); + return await Task.FromResult(xmlRetorno); + } } public string SendRequest(XmlDocument xmlEnvelop, X509Certificate2 certificadoDigital, string url, int timeOut, TipoEvento? tipoEvento = null, string actionUrn = "") @@ -125,12 +125,12 @@ public string SendRequest(XmlDocument xmlEnvelop, X509Certificate2 certificadoDi streamWriter.Write(xmlSoap, 0, Encoding.UTF8.GetBytes(xmlSoap).Length); streamWriter.Close(); - var webResponse = httpWr.GetResponse(); - var respStream = webResponse.GetResponseStream(); - StreamReader streamReader = new StreamReader(respStream); - - string xmlRetorno = streamReader.ReadToEnd(); - return xmlRetorno; + using (HttpWebResponse httpResponse = (HttpWebResponse)httpWr.GetResponse()) + using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) + { + string xmlRetorno = streamReader.ReadToEnd(); + return xmlRetorno; + } } } diff --git a/Shared.MDFe.Utils/Validacao/Validador.cs b/Shared.MDFe.Utils/Validacao/Validador.cs index b1ca4ea2c..3f71da3bb 100644 --- a/Shared.MDFe.Utils/Validacao/Validador.cs +++ b/Shared.MDFe.Utils/Validacao/Validador.cs @@ -30,11 +30,11 @@ /* http://www.zeusautomacao.com.br/ */ /* Rua Comendador Francisco josé da Cunha, 111 - Itabaiana - SE - 49500-000 */ /********************************************************************************/ +using MDFe.Utils.Configuracoes; using System; using System.IO; using System.Xml; using System.Xml.Schema; -using MDFe.Utils.Configuracoes; namespace MDFe.Utils.Validacao { @@ -54,6 +54,8 @@ public static void Valida(string xml, string schema) // Carrega o arquivo de esquema var schemas = new XmlSchemaSet(); + schemas.XmlResolver = new XmlUrlResolver(); + cfg.Schemas = schemas; // Quando carregar o eschema, especificar o namespace que ele valida // e a localização do arquivo