1

I am struggling in a problem I have this scenario:

Production Server

  • Windows Server 2012 R2

  • Internet Information Server 8.5

  • AppPool v4.0

  • 16 GB RAM

  • Intel Xeon(R) E7 (8CPUs)

Project

  • MVC 4 ASP.NET
  • Twilio SMS service
  • ITextSharp (PDF)

In order to send SMS I am using the code below:

 Public Shared Function SendSMS(strNumero As String, strCodigoPais As String, strCodigoGenerado As String) As TipoRespuesta
      Dim objRespuesta As New TipoRespuesta
      Try
        Dim AccountSid As String = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        Dim AuthToken As String = "XXXXXXXXXXXXXXXXXXXXXX"
        Dim NumeroConcatenado As String = "+" & strCodigoPais & strNumero
        Dim MensajeConcatenado As String = "Codigo: " & strCodigoGenerado


        TwilioClient.Init(AccountSid, AuthToken)

        Dim toNumber = New PhoneNumber(NumeroConcatenado)
        Dim message = MessageResource.Create(
          toNumber, from:=New PhoneNumber("+XXXXXXXXXX"),
          body:=MensajeConcatenado,
          messagingServiceSid:="XXXXXXXXXXXXXXXXXXXXX")
      Catch ex As Exception
        objRespuesta.Correcto = False
        objRespuesta.Mensaje = "mensaje: " & ex.Message
      End Try

      objRespuesta.Correcto = True

      Return objRespuesta
    End Function

In order to download PDF files I am using the code below:

 Public Shared Function CrearDesdeHTML(strTextoHTML As String, ByVal strRutaCSS As String, strRutaFuente As String, strUrl As String) As Byte()
      Dim byteRespuesta() As Byte = Nothing
      Dim lstElementos As List(Of Global.iTextSharp.text.IElement) = Nothing
      If System.IO.File.Exists(strRutaFuente) Then
        Global.iTextSharp.text.FontFactory.Register(strRutaFuente, "tipografiaroman")
      End If
      Using pdfDocumento = New Global.iTextSharp.text.Document()
        pdfDocumento.SetMargins(10.0F, 10.0F, 10.0F, 10.0F)
        Using objStrem As New System.IO.MemoryStream()
          Using objWriter As Global.iTextSharp.text.pdf.PdfWriter = Global.iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDocumento, objStrem)
            objWriter.CloseStream = False
            pdfDocumento.Open()
            Dim url As String = strUrl
            Dim jpg As Image = Image.GetInstance(New Uri(url))
            jpg.ScaleToFit(80.0F, 80.0F)
            pdfDocumento.Add(jpg)
            If Not System.IO.File.Exists(strRutaCSS) Then
              Using objStringReader As New System.IO.StringReader(strTextoHTML)

                Global.iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(objWriter, pdfDocumento, objStringReader)
              End Using
            Else
              Using objStreamHTML As System.IO.Stream = New System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(strTextoHTML))
                Using objStremCSS As System.IO.Stream = New System.IO.StreamReader(strRutaCSS).BaseStream
                  Global.iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(objWriter, pdfDocumento, objStreamHTML, objStremCSS)
                End Using
              End Using
            End If
            pdfDocumento.Close()

            objStrem.Position = 0
            byteRespuesta = objStrem.ToArray()
          End Using
        End Using 'MemoryStream
      End Using 'pdfDocument
      Return byteRespuesta
    End Function

And in the View I have this code:

<HttpPost>
    Public Function PDF(<Deserialize> ByVal HTML As String) As ActionResult
      Dim bytePDF() As Byte = Nothing
      Dim strRutaCSS As String = Nothing
      Dim strRutaFontFrutiger As String = Nothing
      Dim strMensaje As String = ""

      Try
        'strRutaCSS = Server.MapPath("~/Content/stylce.css")
        strRutaFontFrutiger = Server.MapPath("~/fonts/xxxxxxxx.ttf")



        bytePDF = CrearDesdeHTML(HTML, strRutaCSS, strRutaFontFrutiger, Parametros.UrlImagen)

      Catch ex As Exception
        strMensaje = ex.Message
      End Try

      If bytePDF Is Nothing Then
        Return "Error"
      Else
        Return File(bytePDF, "application/pdf", "archivo.pdf")
      End If

    End Function

The code works perfectly What is my problem? From time to time for some reason the twilio SMS are not sending I've reviewed the Twilio logs but they aren't there. But the code after is executed correctly.

And the PDF function return this error "The document has no pages."

When I recycle the AppPool everything returns to normal. Twilio messages are sending perfectly and PDFs are downloaded correctly. I don't understand why I have to recycle AppPool in short periods of time to maintain web application working fine. I was thinking is a memory leak problem but I can not identify a specific problem to fix it. What should I do?

The code works well but when web application is published it works temporarily something happens that I can't identify and the PDFs return error, and SMS twilio are not sending. I was thinking that this happens because of some IIS configuration because in IIS 6 there isn´t problem.

EduardoUstarez
  • 603
  • 11
  • 22
  • 1
    Merely the code you pasted is not enough for anyone to tell the cause easily. Use Twilio support service or Microsoft support would be the quickest, as they can analyze all your code and provide guidance. – Lex Li Aug 21 '18 at 05:15
  • Please post code where you call SendSMS – Nick Kovalsky Aug 21 '18 at 05:36
  • You are still talking about **iTextSharp** whereas this name was abandoned two years ago in favor of **iText for .NET**. You are also still using XML Worker, whereas XML Worker has been replaced with pdfHTML (see [Converting HTML to PDF with iText](https://stackoverflow.com/questions/47895935/)). You can get the "Document has no pages" exception if there's something wrong with `strTextoHTML` (e.g. if it doesn't have any content). The old iTextSharp also doesn't clean up "dirty HTML." You need iText 7 and the pdfHTML add-on to do that. – Bruno Lowagie Aug 21 '18 at 11:13
  • 1
    In short: if this is an iText problem (but that's not clear from your question), you won't get many answers because you are using a version of iText that is no longer supported. You should upgrade to the latest version: https://www.nuget.org/packages/itext7/ – Bruno Lowagie Aug 21 '18 at 11:14
  • The code above works well. But in production when there are many user downloading PDF files something happens that it stops working. I have to recycle the apppool to fix it. – EduardoUstarez Aug 21 '18 at 14:21
  • Since you are using old software, is there a chance that it has a memory leak which eventually collapses your apppool? I would try to update iTextSharp as Bruno has suggested and see if you continue to have the same issue. – philnash Aug 22 '18 at 05:15
  • I've moved the SMS code and the PDF code to other server using SOAP services and it fixed the problem and saved the day. – EduardoUstarez Aug 23 '18 at 21:26

0 Answers0