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.