public NFeDistribuicaoDFeSoapClient(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) : base(NFeDistribuicaoDFeSoapClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress) { this.Endpoint.Name = endpointConfiguration.ToString(); ConfigureEndpoint(this.Endpoint, this.ClientCredentials); }
/// <summary> /// Faz a chamada para o webservice de distribuição de NFe /// </summary> /// <param name="cUF">Código da UF (IBGE)</param> /// <param name="cnpj">CNPJ da empresa (14 dígitos - sem máscara)</param> /// <param name="dados">Dados que serão enviados para o webservice</param> private async Task <Models.NFe.retDistDFeInt> ChamarWsNFe(string cUF, string cnpj, object dados) { if (DateTime.UtcNow > _Certificado.NotAfter) { throw new Exception($"O certificado venceu em {_Certificado.NotAfter}!"); } if (DateTime.UtcNow < _Certificado.NotBefore) { throw new Exception("O certificado ainda não é válido!"); } // Configuração do ws var endpoint = new System.ServiceModel.EndpointAddress(_EndPointNFeDistribuicaoDFe); var binding = new System.ServiceModel.BasicHttpBinding(System.ServiceModel.BasicHttpSecurityMode.Transport) { MaxReceivedMessageSize = 999999999 }; binding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Certificate; // Instância do cliente var ws = new NFeDistribuicaoDFeSoapClient(binding, endpoint); try { // Definição do certificado ws.ClientCredentials.ClientCertificate.Certificate = _Certificado; // Dados Models.NFe.TCodUfIBGE eUF; if (!Models.NFe.TCodUfIBGE.TryParse("Item" + cUF, out eUF)) { throw new ArgumentException("Código IBGE da UF Inválido!", nameof(cUF)); } using var memoryStream = new MemoryStream(); using var streamWriter = new StreamWriter(memoryStream); new XmlSerializer(typeof(Models.NFe.distDFeInt)).Serialize(streamWriter, new Models.NFe.distDFeInt { tpAmb = _Ambiente, cUFAutor = eUF, cUFAutorSpecified = true, ItemElementName = cnpj.Length > 11 ? Models.NFe.TipoPessoa.CNPJ : Models.NFe.TipoPessoa.CPF, CpjCnpj = cnpj, Item1 = dados, versao = Models.NFe.TVerDistDFe.Item101, }); var xmlDocument = new XmlDocument(); xmlDocument.LoadXml(Encoding.UTF8.GetString(memoryStream.ToArray())); // Chama o web service var resposta = await ws.nfeDistDFeInteresseAsync(xmlDocument.DocumentElement); // Trabalha com a resposta return(resposta.Body.nfeDistDFeInteresseResult.DeserializeTo <Models.NFe.retDistDFeInt>()); } finally { await ws.CloseAsync(); } }
public NFeDistribuicaoDFeSoapClient(EndpointConfiguration endpointConfiguration) : base(NFeDistribuicaoDFeSoapClient.GetBindingForEndpoint(endpointConfiguration), NFeDistribuicaoDFeSoapClient.GetEndpointAddress(endpointConfiguration)) { this.Endpoint.Name = endpointConfiguration.ToString(); ConfigureEndpoint(this.Endpoint, this.ClientCredentials); }