Thanks for referencing my example page, Leo!
I also created some time ago a page with tips on how to access the eSocial service, including some of the ones you posted in your question and answer. I'll take advantage of the subject of your question to put these tips here, to help anyone with similar problems.
To access the eSocial submission service, in the Restricted Production environment, the URL should be this:
link
In addition, according to the eSocial Developer's Guide v1.6.3, page 83, item '7.9. You should also install on the machine that will access the Chain of Certificates service issued on 06/02/2017 by Serpro, which are 3 certificates that can be obtained at this address:
>
link
According to the 02.03 item of the eSocial Portal FAQ page, certificates must be installed in the order that they are arranged on this Serpro page, and:
The Certification Authority Brazilian root v5 must be installed in the root CA repository. The SERPRO Certification Authority v4 and SERPRO Certification Authority Final v5 must be installed in the intermediate CA repository.
Remember that it is also necessary to have a valid digital certificate (A1 or A3, e-CNPJ or e-CPF) installed on the computer that will access the web service, which must be used to access the service. One tip: When I started the tests with eSocial, I was almost a week banging my head to get the first access, when I finally discovered that in my case (e-CNPJ A1), it was necessary to select the Mark this key how to exportable and store ) and install my certificate in the store Personal / em>), Current User and Current User
Regarding the code used to access the service, the binding service (I used BasicHttpBinding
or BasicHttpsBinding
) should be configured to use SecurityMode = Transport (for HTTPS) and ClientCredentialType = Certificate (to specify a certificate), something like this:
var urlServicoEnvio = @"https://webservices.producaorestrita.esocial.gov.br/servicos/empregador/enviarloteeventos/WsEnviarLoteEventos.svc";
var address = new EndpointAddress(urlServicoEnvio);
var binding = new BasicHttpsBinding(); //Disponível desde .NET Framework 4.5
// ou:
//var binding = new BasicHttpBinding(BasicHttpsSecurityMode.Transport);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
var wsClient = new WsEnviar.ServicoEnviarLoteEventosClient(binding, address);
wsClient.ClientCredentials.ClientCertificate.Certificate = x509Cert;
var retornoEnvioXElement = wsClient.EnviarLoteEventos(loteEventosXDoc.Root);
wsClient.Close();
As for the WsEnviar.ServicoEnviarLoteEventosClient
class used in code, it was created by Visual Studio when adding a Service Reference , using the same URL as the service, but adding the singleWsdl in>:
link
In VS it is also possible to add a reference to the service by directly using the WsSubmitEvents-v1_1_0.wsdl file available in the eSocial Communication Package (latest version 1.4.1) , which can be found on the technical documentation page of the eSocial Portal.
This tool, Add Service Reference , will create a client class to consume the web service, in the case of the WsEnviar.ServicoEnviarLoteEventosClient
sample, which inherits the System.ServiceModel.ClientBase class .
You can also use the svcutil.exe command line tool, which will likewise create a client class inheriting the System.ServiceModel.ClientBase class. Alternatively, you can also use the older wsdl.exe command line tool for ASMX-based .NET Framework 2 time services that will also create a client class, but this time inheriting the System.Web.Services.Protocols.SoapHttpClientProtocol class. But in that case the code to consume the service would be a bit different.
Following all these steps, access to the eSocial web service should work.