How to consume EFD-Reinf Webservice in C#? (Sending Events)

Asked

Viewed 524 times

3

I am initiating the EFD-Reinf Information Submission Tests in C#.
By Visual Studio, I added a Service Reference in my project with the URL:

https://preprodefdreinf.receita.fazenda.gov.br/wsreinf/RecepcaoLoteReinf.svc?singleWsdl

I have the digital certificate installed on my machine.
He requests permission to use the key:

Permissão da chave

It loads the available services, click OK and the service is available to me. I can instantiate, etc.

I ask, how should I proceed from there?

Because with other Webservices, like the Post Office, I did this way: instantiated, passed the parameters and WS returned me with the answer, and with the EFD-Reinf I do not know how to pass such parameters.

For those who have already developed, this is the best way to send the events of EFD-Reinf?

1 answer

2


The main code to access the EFD-Reinf Webservice is this:

using System.Net;
using System.ServiceModel;
using System.Security.Cryptography.X509Certificates;
using System.Xml.Linq;

public void EnviarLoteReinf()
{
   // Carrega o certificado digital a partir de um arquivo PFX, informando a senha.
   X509Certificate2 x509Cert = new X509Certificate2(caminhoArquivoPfx, senhaArquivoPfx);
   // Carrega o XML de lote a partir de um arquivo.
   // Mas os XMLs dos eventos devem ser assinados digitalmente antes de inseridos no XML de lote.
   // Para isso é possível usar a função SignXmlDoc() disponível na resposta abaixo:
   // /a/277476/
   XDocument loteEventosXDoc = XDocument.Load(caminhoArquivoXml);

   var urlServicoEnvio = @"https://preprodefdreinf.receita.fazenda.gov.br/wsreinf/RecepcaoLoteReinf.svc";
   var address = new EndpointAddress(urlServicoEnvio);
   // BasicHttpsBinding está disponível somente a partir do .NET Framework 4.5.
   // Se estiver usando uma versão anterior, use a linha comentada abaixo.
   //var binding = new BasicHttpBinding(BasicHttpsSecurityMode.Transport);
   var binding = new BasicHttpsBinding();

   // Informa que será usado um certificado digital para acessar o serviço.
   binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

   // Veja essa pergunta para mais detalhes:
   // /q/318351/
   ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

   // Cria o objeto cliente (do tipo System.ServiceModel.ClientBase) para acesso ao WebService.
   var wsClient = new ServiceReference2.RecepcaoLoteReinfClient(binding, address);
   // Passa o certificado digital para o objeto do tipo System.ServiceModel.ClientBase.
   wsClient.ClientCredentials.ClientCertificate.Certificate = x509Cert;

   // Veja: https://stackoverflow.com/a/49303859/
   wsClient.Open();
   // Chama o WebService de fato, passando o XML do lote.
   // O método espera um objeto do tipo XElement, e retorna outro objeto XElement.
   var retornoEnvioXElement = wsClient.ReceberLoteEventos(loteEventosXDoc.Root);
   wsClient.Close();
}

Some important points (and others not so much) that are partially commented on in the code, or are related:

  • The XML that will be sent to the service is a batch file that will contain one or more EFD-Reinf events. These event Xmls must be signed individually before they are inserted into the lot. reply there is an example of function to sign XML using digital certificate;
  • The class System.ServiceModel.BasicHttpsBinding is only available from . NET Framework 4.5, if you are using a previous version, use the class BasicHttpBinding;
  • For more details about the line ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls, see that question;
  • For interesting information on System.ServiceModel.ClientBase.Open() see that answer;
  • If you want to see an example of handling exceptions returned by a WCF Webservice, see that answer.

Here are some useful links to work with EFD-Reinf:

  • Thank you so much Pedro!!. I got it.! I just had to migrate from Visual Studio 2010 to Visual Studio 2017 because of the framework!. Just returning a batch version error: <Description>Invalid batch version. Use version 1.04.00. </Description>. Should I open another question or continue in this post?

  • 1

    Vagner, good that it worked! But the best way to thank for a reply is to mark it as accepted by clicking on the visa sign ( ). When you reach 15 reputation points you can also vote in favour of an answer or question. See: Someone answered me and Why vote?. Also do the [tour], for an initial explanation of how the site works.

  • You better open up another question, Vagner, because it’s another entirely different question. But you can put a link to that question, in your new question, to better contextualize and not need to repeat text and code.

  • Although, in this my reply, has the link to the XSD Schemes at the end, and there is a link to the XSD Communication Package v1.4, which is the most current and what should be used. It will probably just be you upgrade to this schema which should work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.