static int FindMapping(SoapHeaderMapping[] mappings, SoapHeader header, SoapHeaderDirection direction) { if (mappings == null || mappings.Length == 0) { return(-1); } Type headerType = header.GetType(); for (int i = 0; i < mappings.Length; i++) { SoapHeaderMapping mapping = mappings[i]; if ((mapping.direction & direction) == 0) { continue; } if (!mapping.custom) { continue; } if (mapping.headerType.IsAssignableFrom(headerType)) { return(i); } } return(-1); }
private static string GetHeaderElementName(SoapHeader header) { if (header is SoapUnknownHeader) { return ((SoapUnknownHeader) header).Element.LocalName; } return GetHeaderElementName(header.GetType()); }
private static string GetHeaderElementName(SoapHeader header) { if (header is SoapUnknownHeader) { return(((SoapUnknownHeader)header).Element.LocalName); } return(GetHeaderElementName(header.GetType())); }
private static int FindMapping(SoapHeaderMapping[] mappings, SoapHeader header, SoapHeaderDirection direction) { if ((mappings != null) && (mappings.Length != 0)) { Type c = header.GetType(); for (int i = 0; i < mappings.Length; i++) { SoapHeaderMapping mapping = mappings[i]; if ((((mapping.direction & direction) != 0) && mapping.custom) && mapping.headerType.IsAssignableFrom(c)) { return i; } } } return -1; }
private static int FindMapping(SoapHeaderMapping[] mappings, SoapHeader header, SoapHeaderDirection direction) { if ((mappings != null) && (mappings.Length != 0)) { Type c = header.GetType(); for (int i = 0; i < mappings.Length; i++) { SoapHeaderMapping mapping = mappings[i]; if ((((mapping.direction & direction) != 0) && mapping.custom) && mapping.headerType.IsAssignableFrom(c)) { return(i); } } } return(-1); }
public object[] GetHeaderValueArray(SoapHeaderDirection dir, SoapHeaderCollection headers) { HeaderInfo[] headerInfos = GetHeaders(dir); if (headerInfos == null) { return(null); } object[] hs = new object [headerInfos.Length]; for (int n = 0; n < headers.Count; n++) { SoapHeader h = headers[n]; Type t = h.GetType(); for (int i = 0; i < headerInfos.Length; i++) { if (headerInfos [i].HeaderType == t) { hs [i] = h; } } } return(hs); }
internal static void SetHeaderMembers(SoapHeaderCollection headers, object target, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, bool client) { bool[] headerHandled = new bool[headers.Count]; for (int i = 0; i < mappings.Length; i++) { SoapHeaderMapping mapping = mappings[i]; if ((mapping.direction & direction) == 0) { continue; } if (mapping.repeats) { ArrayList list = new ArrayList(); for (int j = 0; j < headers.Count; j++) { SoapHeader header = headers[j]; if (headerHandled[j]) { continue; } if (mapping.headerType.IsAssignableFrom(header.GetType())) { list.Add(header); headerHandled[j] = true; } } MemberHelper.SetValue(mapping.memberInfo, target, list.ToArray(mapping.headerType)); } else { bool handled = false; for (int j = 0; j < headers.Count; j++) { SoapHeader header = headers[j]; if (headerHandled[j]) { continue; } if (mapping.headerType.IsAssignableFrom(header.GetType())) { if (handled) { header.DidUnderstand = false; continue; } handled = true; MemberHelper.SetValue(mapping.memberInfo, target, header); headerHandled[j] = true; } } } } if (client) { for (int i = 0; i < headerHandled.Length; i++) { if (!headerHandled[i]) { SoapHeader header = headers[i]; if (header.MustUnderstand && !header.DidUnderstand) { throw new SoapHeaderException(Res.GetString(Res.WebCannotUnderstandHeader, GetHeaderElementName(header)), new XmlQualifiedName(Soap.MustUnderstandCode, Soap.Namespace)); } } } } }
internal static void WriteHeaders(XmlWriter writer, XmlSerializer serializer, SoapHeaderCollection headers, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, bool isEncoded, string defaultNs, bool serviceDefaultIsEncoded, string envelopeNs) { if (headers.Count == 0) { return; } if (isEncoded && writer is XmlSpecialTextWriter) { ((XmlSpecialTextWriter)writer).EncodeIds = true; } writer.WriteStartElement(Soap.Header, envelopeNs); // SOAP12: always soap 1.1, not using encodingStyle; //SoapProtocolVersion version; SoapProtocolVersion version = SoapProtocolVersion.Soap11; // SOAP12: not using encodingStyle /*string encodingStyle; * if (envelopeNs == Soap12.Namespace) { * version = SoapProtocolVersion.Soap12; * encodingStyle = Soap12.Encoding; * } * else { * version = SoapProtocolVersion.Soap11; * encodingStyle = Soap.Encoding; * }*/ int unknownHeaderCount = 0; ArrayList otherHeaders = new ArrayList(); SoapHeader[] headerArray = new SoapHeader[mappings.Length]; bool[] headerSet = new bool[headerArray.Length]; for (int i = 0; i < headers.Count; i++) { SoapHeader header = headers[i]; if (header == null) { continue; } int headerPosition; header.version = version; if (header is SoapUnknownHeader) { otherHeaders.Add(header); unknownHeaderCount++; } else if ((headerPosition = FindMapping(mappings, header, direction)) >= 0 && !headerSet[headerPosition]) { headerArray[headerPosition] = header; headerSet[headerPosition] = true; } else { otherHeaders.Add(header); } } int otherHeaderCount = otherHeaders.Count - unknownHeaderCount; if (isEncoded && otherHeaderCount > 0) { SoapHeader[] newHeaderArray = new SoapHeader[mappings.Length + otherHeaderCount]; headerArray.CopyTo(newHeaderArray, 0); // fill in the non-statically known headers (otherHeaders) starting after the statically-known ones int count = mappings.Length; for (int i = 0; i < otherHeaders.Count; i++) { if (!(otherHeaders[i] is SoapUnknownHeader)) { newHeaderArray[count++] = (SoapHeader)otherHeaders[i]; } } headerArray = newHeaderArray; } // SOAP12: not using encodingStyle //serializer.Serialize(writer, headerArray, null, isEncoded ? encodingStyle : null); serializer.Serialize(writer, headerArray, null); foreach (SoapHeader header in otherHeaders) { if (header is SoapUnknownHeader) { SoapUnknownHeader unknown = (SoapUnknownHeader)header; if (unknown.Element != null) { unknown.Element.WriteTo(writer); } } else if (!isEncoded) // encoded headers already appended to members mapping { string ns = SoapReflector.GetLiteralNamespace(defaultNs, serviceDefaultIsEncoded); new XmlSerializer(header.GetType(), ns).Serialize(writer, header); } } // reset the soap version for (int i = 0; i < headers.Count; i++) { SoapHeader header = headers[i]; if (header != null) { header.version = SoapProtocolVersion.Default; } } writer.WriteEndElement(); writer.Flush(); if (isEncoded && writer is XmlSpecialTextWriter) { ((XmlSpecialTextWriter)writer).EncodeIds = false; } }
static int FindMapping(SoapHeaderMapping[] mappings, SoapHeader header, SoapHeaderDirection direction) { if (mappings == null || mappings.Length == 0) return -1; Type headerType = header.GetType(); for (int i = 0; i < mappings.Length; i++) { SoapHeaderMapping mapping = mappings[i]; if ((mapping.direction & direction) == 0) continue; if (!mapping.custom) continue; if (mapping.headerType.IsAssignableFrom(headerType)) { return i; } } return -1; }
public static void WriteHeaders(XmlWriter writer, XmlSerializer serializer, SoapHeaderCollection headers, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, bool isEncoded, string defaultNS, bool serviceDefaultIsEncoded, string envelopeNS) { if (headers.Count == 0) { return; } writer.WriteStartElement(Soap.Element.Header, envelopeNS); SoapProtocolVersion version; string encodingStyle; if (envelopeNS == Soap12.Namespace) { version = SoapProtocolVersion.Soap12; encodingStyle = Soap12.Encoding; } else { version = SoapProtocolVersion.Soap11; encodingStyle = Soap.Encoding; } int unknownHeaderCount = 0; ArrayList otherHeaders = new ArrayList(); SoapHeader[] headerArray = new SoapHeader[mappings.Length]; bool[] headerSet = new bool[headerArray.Length]; for (int i = 0; i < headers.Count; i++) { SoapHeader header = headers[i]; if (header == null) { continue; } int headerPosition; header.version = version; if (header is SoapUnknownHeader) { otherHeaders.Add(header); unknownHeaderCount++; } else if ((headerPosition = FindMapping(mappings, header, direction)) >= 0 && !headerSet[headerPosition]) { headerArray[headerPosition] = header; headerSet[headerPosition] = true; } else { otherHeaders.Add(header); } } int otherHeaderCount = otherHeaders.Count - unknownHeaderCount; if (isEncoded && otherHeaderCount > 0) { SoapHeader[] newHeaderArray = new SoapHeader[mappings.Length + otherHeaderCount]; headerArray.CopyTo(newHeaderArray, 0); // fill in the non-statically known headers (otherHeaders) starting after the statically-known ones int count = mappings.Length; for (int i = 0; i < otherHeaders.Count; i++) { if (!(otherHeaders[i] is SoapUnknownHeader)) { newHeaderArray[count++] = (SoapHeader)otherHeaders[i]; } } headerArray = newHeaderArray; } TraceMethod caller = Tracing.On ? new TraceMethod(typeof(SoapHeaderHandling), "WriteHeaders") : null; if (Tracing.On) { Tracing.Enter(Tracing.TraceId(Res.TraceWriteHeaders), caller, new TraceMethod(serializer, "Serialize", writer, headerArray, null, isEncoded ? encodingStyle : null, "h_")); } serializer.Serialize(writer, headerArray, null, isEncoded ? encodingStyle : null, "h_"); if (Tracing.On) { Tracing.Exit(Tracing.TraceId(Res.TraceWriteHeaders), caller); } foreach (SoapHeader header in otherHeaders) { if (header is SoapUnknownHeader) { SoapUnknownHeader unknown = (SoapUnknownHeader)header; if (unknown.Element != null) { unknown.Element.WriteTo(writer); } } else if (!isEncoded) // encoded headers already appended to members mapping { string ns = SoapReflector.GetLiteralNamespace(defaultNS, serviceDefaultIsEncoded); XmlSerializer headerSerializer = new XmlSerializer(header.GetType(), ns); if (Tracing.On) { Tracing.Enter(Tracing.TraceId(Res.TraceWriteHeaders), caller, new TraceMethod(headerSerializer, "Serialize", writer, header)); } headerSerializer.Serialize(writer, header); if (Tracing.On) { Tracing.Exit(Tracing.TraceId(Res.TraceWriteHeaders), caller); } } } // reset the soap version for (int i = 0; i < headers.Count; i++) { SoapHeader header = headers[i]; if (header != null) { header.version = SoapProtocolVersion.Default; } } writer.WriteEndElement(); writer.Flush(); }
public static System.Web.Services.Protocols.SoapHttpClientProtocol ClientProxyFactory( Parametro oParam, TServico TipoServico) { String ClassName = ""; try { string nomeClasse = string.Empty; //buscar nome do metodo pelo tServico foreach (ClasseServico atr in TipoServico.GetType().GetField(TipoServico.ToString()).GetCustomAttributes(typeof(ClasseServico), false)) { if (String.IsNullOrEmpty(atr.value)) { throw new Exception("Serviço não esta associado com nenhuma classe cliente."); } nomeClasse = atr.value; } //particularidades if (oParam.UF == TCodUfIBGE.Bahia && oParam.tipoEmissao == TNFeInfNFeIdeTpEmis.Normal)//Bahia { if (TipoServico == TServico.Status && oParam.versao == VersaoXML.NFe_v310) { nomeClasse = "NfeStatusServico"; } if (TipoServico == TServico.Consulta && oParam.versao == VersaoXML.NFe_v310) { nomeClasse = "NfeConsulta"; } if (TipoServico == TServico.Inutilizacao && oParam.versao == VersaoXML.NFe_v310) { nomeClasse = "NfeInutilizacao"; } } ClassName = "RDI.NFe2.Business." + GetAmbWebService(oParam, TipoServico) + "." + TipoServico.ToString() + "."; String headerClassName = ClassName + "nfeCabecMsg"; ClassName += nomeClasse; Type classType = GetMyAssembly().GetType(ClassName); if (classType == null) { throw new Exception("Não foi possível definir o tipo do cliente de webservice. #ClientProxyFactory"); } System.Web.Services.Protocols.SoapHttpClientProtocol oServico = (System.Web.Services.Protocols.SoapHttpClientProtocol)System.Activator.CreateInstance(classType); if (TipoServico != TServico.ConsultaDFe) //ConsultaDFe não tem header { #region Instancia cabecalho Type headerClassType = GetMyAssembly().GetType(headerClassName); if (headerClassType == null) { throw new Exception("Não foi possível definir o tipo do header do cliente de webservice. #ClientProxyFactory"); } System.Web.Services.Protocols.SoapHeader oCabecalho = (System.Web.Services.Protocols.SoapHeader)System.Activator.CreateInstance(headerClassType); if ((TipoServico == TServico.ManifestacaoDestinatario) || //ManifestacaoDestinatario deverá usar AN 91 (TipoServico == TServico.DownloadNF)) //DownloadNF deverá usar AN 91 { oCabecalho.GetType().GetProperty("cUF").SetValue(oCabecalho, "91", null); } else { oCabecalho.GetType().GetProperty("cUF").SetValue(oCabecalho, ((System.Xml.Serialization.XmlEnumAttribute)oParam.UF.GetType().GetField( oParam.UF.ToString()).GetCustomAttributes( typeof(System.Xml.Serialization.XmlEnumAttribute), false)[0]).Name, null); } string versao = oParam.versaoDados; //particularidade para ConsSitNFe usando v200 if (TipoServico == TServico.Consulta && oParam.versao == VersaoXML.NFe_v200) { versao = "2.01"; } //Particularidade para RecepcaoEvento if (TipoServico == TServico.RecepcaoEvento) { versao = oParam.versaoDadosEventos; } //particularidade para consultaCadastro if (TipoServico == TServico.Cadastro) { versao = "2.00"; } //particularidade para manifestacao destinatario if (TipoServico == TServico.ManifestacaoDestinatario || TipoServico == TServico.DownloadNF) { versao = "1.00"; } oCabecalho.GetType().GetProperty("versaoDados").SetValue(oCabecalho, versao, null); oServico.GetType().GetProperty("nfeCabecMsgValue").SetValue(oServico, oCabecalho, null); #endregion } return(oServico); } catch (Exception ex) { throw new Exception("ClientProxyFactory # não foi possível criar o cliente (" + ClassName + ") para acesso aos webservices da SEFAZ. InnerException: " + ex.Message); } }
public static void SetHeaderMembers(SoapHeaderCollection headers, object target, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, bool client) { bool[] flagArray = new bool[headers.Count]; if (mappings != null) { for (int j = 0; j < mappings.Length; j++) { SoapHeaderMapping mapping = mappings[j]; if ((mapping.direction & direction) != 0) { if (mapping.repeats) { ArrayList list = new ArrayList(); for (int k = 0; k < headers.Count; k++) { SoapHeader header = headers[k]; if (!flagArray[k] && mapping.headerType.IsAssignableFrom(header.GetType())) { list.Add(header); flagArray[k] = true; } } MemberHelper.SetValue(mapping.memberInfo, target, list.ToArray(mapping.headerType)); } else { bool flag = false; for (int m = 0; m < headers.Count; m++) { SoapHeader header2 = headers[m]; if (!flagArray[m] && mapping.headerType.IsAssignableFrom(header2.GetType())) { if (flag) { header2.DidUnderstand = false; } else { flag = true; MemberHelper.SetValue(mapping.memberInfo, target, header2); flagArray[m] = true; } } } } } } } for (int i = 0; i < flagArray.Length; i++) { if (!flagArray[i]) { SoapHeader header3 = headers[i]; if (header3.MustUnderstand && !header3.DidUnderstand) { throw new SoapHeaderException(System.Web.Services.Res.GetString("WebCannotUnderstandHeader", new object[] { GetHeaderElementName(header3) }), new XmlQualifiedName("MustUnderstand", "http://schemas.xmlsoap.org/soap/envelope/")); } } } }
public static System.Web.Services.Protocols.SoapHttpClientProtocol ClientProxyFactory(Parametro oParam, TService TipoServico) { if (oParam.versao == VersaoXML.NFe_v400 && (TipoServico == TService.Autorizacao || TipoServico == TService.RetAutorizacao || TipoServico == TService.ConsultaProtocolo || TipoServico == TService.Inutilizacao || TipoServico == TService.RecepcaoEvento || TipoServico == TService.Cadastro || TipoServico == TService.Status)) { if (oParam.conexao == TipoConexao.NFe) { var tServer = TServer.NaoMapeado; string Ambiente = oParam.tipoAmbiente.ToString(); var AtendidoPor = (NFe_AtendidoPor)typeof(TCodUfIBGE).GetField(oParam.UF.ToString()).GetCustomAttributes(typeof(NFe_AtendidoPor), false).FirstOrDefault(); if (AtendidoPor == null) { throw new Exception("UF não está associado com nenhum Servidor Autorizador."); } if (oParam.tipoEmissao == TNFeInfNFeIdeTpEmis.ContingenciaSVCAN || oParam.tipoEmissao == TNFeInfNFeIdeTpEmis.ContingenciaSVCRS) { tServer = AtendidoPor.ServidorAutorizadorSVC; } else { tServer = AtendidoPor.ServidorAutorizador; } return(RDI.NFe2.Webservices.WSUtils.SoapHttpClientFactory(tServer, Ambiente, TipoServico)); } else if (oParam.conexao == TipoConexao.NFCe) { var AtendidoPor = (NFCe_AtendidoPor)typeof(TCodUfIBGE).GetField(oParam.UF.ToString()).GetCustomAttributes(typeof(NFCe_AtendidoPor), false).FirstOrDefault(); if (AtendidoPor == null) { throw new Exception("UF não está associado com nenhum Servidor Autorizador."); } string Ambiente = oParam.tipoAmbiente.ToString(); return(RDI.NFe2.Webservices.WSUtils.SoapHttpClientFactory(AtendidoPor.ServidorAutorizador, Ambiente, TipoServico)); } else { throw new Exception("UF não está associado com nenhum Servidor Autorizador."); } } else { //TODO : enviar todos os webservices ativos para o projeto rdi.nfe.webservices após a desativação da versao 3.10 String ClassName = ""; try { string nomeClasse = string.Empty; //buscar nome do metodo pelo tServico foreach (ClasseServico atr in TipoServico.GetType().GetField(TipoServico.ToString()).GetCustomAttributes(typeof(ClasseServico), false)) { if (String.IsNullOrEmpty(atr.NomeClasse)) { throw new Exception("Serviço não esta associado com nenhuma classe cliente."); } nomeClasse = atr.NomeClasse; } //particularidades if (oParam.UF == TCodUfIBGE.Bahia && oParam.tipoEmissao == TNFeInfNFeIdeTpEmis.Normal && oParam.conexao == TipoConexao.NFe)//Bahia { if (TipoServico == TService.Status && oParam.versao == VersaoXML.NFe_v310) { nomeClasse = "NfeStatusServico"; } if (TipoServico == TService.ConsultaProtocolo && oParam.versao == VersaoXML.NFe_v310) { nomeClasse = "NfeConsulta"; } if (TipoServico == TService.Inutilizacao && oParam.versao == VersaoXML.NFe_v310) { nomeClasse = "NfeInutilizacao"; } } var subNamespace = TipoServico.ToString(); if (TipoServico == TService.ConsultaProtocolo) { subNamespace = "Consulta"; } else if (TipoServico == TService.ManifestacaoDestinatario || TipoServico == TService.EPEC) { subNamespace = "Eventos"; } ClassName = "RDI.NFe2.Business." + GetAmbWebService(oParam, TipoServico) + "." + subNamespace + "."; String headerClassName = ClassName + "nfeCabecMsg"; ClassName += nomeClasse; Type classType = GetMyAssembly().GetType(ClassName); if (classType == null) { throw new Exception("Não foi possível definir o tipo do cliente de webservice. #ClientProxyFactory"); } System.Web.Services.Protocols.SoapHttpClientProtocol oServico = (System.Web.Services.Protocols.SoapHttpClientProtocol)System.Activator.CreateInstance(classType); if (TipoServico != TService.ConsultaDFe) //ConsultaDFe não tem header { #region Instancia cabecalho Type headerClassType = GetMyAssembly().GetType(headerClassName); if (headerClassType == null) { throw new Exception("Não foi possível definir o tipo do header do cliente de webservice. #ClientProxyFactory"); } System.Web.Services.Protocols.SoapHeader oCabecalho = (System.Web.Services.Protocols.SoapHeader)System.Activator.CreateInstance(headerClassType); if ((TipoServico == TService.ManifestacaoDestinatario) || //ManifestacaoDestinatario deverá usar AN 91 (TipoServico == TService.DownloadNF) || //DownloadNF deverá usar AN 91 (TipoServico == TService.EPEC)) //DownloadNF deverá usar AN 91 { oCabecalho.GetType().GetProperty("cUF").SetValue(oCabecalho, "91", null); } else { oCabecalho.GetType().GetProperty("cUF").SetValue(oCabecalho, ((System.Xml.Serialization.XmlEnumAttribute)oParam.UF.GetType().GetField( oParam.UF.ToString()).GetCustomAttributes( typeof(System.Xml.Serialization.XmlEnumAttribute), false)[0]).Name, null); } string versao = oParam.versaoDados; //particularidade para ConsSitNFe usando v200 if (TipoServico == TService.ConsultaProtocolo && oParam.versao == VersaoXML.NFe_v200) { versao = "2.01"; } //Particularidade para RecepcaoEvento if (TipoServico == TService.RecepcaoEvento) { versao = oParam.versaoDadosEventos; } //particularidade para consultaCadastro if (TipoServico == TService.Cadastro) { versao = "2.00"; } //particularidade para manifestacao destinatario if (TipoServico == TService.ManifestacaoDestinatario || TipoServico == TService.EPEC || TipoServico == TService.DownloadNF) { versao = "1.00"; } oCabecalho.GetType().GetProperty("versaoDados").SetValue(oCabecalho, versao, null); oServico.GetType().GetProperty("nfeCabecMsgValue").SetValue(oServico, oCabecalho, null); #endregion } return(oServico); } catch (Exception ex) { throw new Exception("ClientProxyFactory # não foi possível criar o cliente (" + ClassName + ") para acesso aos webservices da SEFAZ. InnerException: " + ex.Message); } } }