Unable to read data from the transport connection. FtpWebRequest. C#

136 Views Asked by At

I'm configuring a service to read some csv files from a ftp repository. When I debug in my local machine it works perfectly but when I build it and make it run, It doesn't work. I put some console logs to write the steps the program takes. I could see it connects to the ftp and reads its files but not the whole content of it. Example: the first file has 170000 lines, the program will read only the first 489 ones and then the message: "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host" comes to me. It runs by itself three more times, and I know it because the same message comes 4 times but no record is commited. Below there is part of the code.

    public async Task<IResult> GravaSore(List<string> arquivos) //caller
    {
            foreach (var item in arquivos)
            {
                await ProcessaGravacao(item);
            }
            return new TypeResult<bool>("processados"); ;
    }



    private async Task<IResult> ProcessaGravacao(string item) //called
    {
        var urlFtp = [ftp_string];

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(urlFtp);
        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
        request.Credentials = new NetworkCredential("usr", "pwd");
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.EnableSsl = true;
        request.UsePassive = true;

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        String linha;

        try
        {
            var contador = 0;
            while ((linha = reader.ReadLine()) != null)
            {
                if (!linha.Contains("cpf"))
                {
                    var colunas = linha.Split(";");
                    var cpf = colunas[0];
                    var core = colunas[1].Substring(0, 5);
                    var core2 = decimal.Parse(core);
                    var data = colunas[2].Split("-");
                    var ano = int.Parse(data[0]);
                    var mes = int.Parse(data[1]);
                    var dataCore = new DateTime(ano, mes, 01, 01, 01, 01);
                    var pessoa = _pRepositorio.OPPCpf(cpf);
                    if (pessoa == null)
                        break;
                    var cliente = pessoa.Cliente;

                    Csore entity = new Csore { DataHora = dataCore, Cliente = cliente, Core = core2 };
                    await _Repositorio.AddAsync(entity);

                    contador++;
                    if (contador >= 5000)
                    {
                        contador = 0;
                        await _Repositorio.CommitAsync();
                    }
                }
            }
            if(contador > 0)
                await _Repositorio.CommitAsync();
            Console.WriteLine("sucesso");

            reader.Close();
            response.Close();
            return new TypeResult<bool>("Sucesso");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return null;
        }
    }
0

There are 0 best solutions below