I'm lost in threading-wasteland. Cannot create record without errors after creating invoice.pdf RRS feed

  • Question

  • Hi All,

    I'm trying to get the following scenario to work:

    - when updating the status of an order to 'fullfilled' automatically create invoice.pdf, save the invoice to the database, show example email to enduser.

    This is what I did:

    On the order_inserting event I call the function to create the pdf and to insert the result in FileTable:

    Private Sub Orders_Updating(entity As Order) If entity.orderStatusID = 4 Then Dim myReportUploader As New LightSwitchApplication.Reports.ReportViewer() Me.Details.Dispatcher.BeginInvoke(Sub() Dim myReportBytes As Byte() myReportBytes = myReportUploader.UploadReportOrder("Invoice", entity)

    Using serverContext As ServerApplicationContext = ServerApplicationContext.CreateContext() Dim fileMetaData As FileMetaData = serverContext.DataWorkspace.HoubenData.FileMetaDatas.AddNew() fileMetaData.fileName = entity.orderFactuurID & ".pdf" fileMetaData.fileUploadDateTime = DateTime.Now fileMetaData.fileStore = myReportBytes fileMetaData.fileSize = 0 serverContext.DataWorkspace.HoubenData.SaveChanges() End Using End Sub) End If End Sub

    Which in turn calls the UploadReportOrder function which is part of reportviewer.aspx.vb (resides in the serverproject as well). 

    Public Function UploadReportOrder(strReportName As String, myOrder As Order) As Byte() 'Load the .rdlc that contains the report Dim warnings As Warning() = Nothing Dim streamids As String() = Nothing Dim mimeType As String = Nothing Dim encoding As String = Nothing Dim extension As String = Nothing Dim deviceInfo As String Dim reportBytes As Byte() Dim myReportViewer As Microsoft.Reporting.WebForms.ReportViewer = New Microsoft.Reporting.WebForms.ReportViewer() myReportViewer.Reset() myReportViewer.ProcessingMode = ProcessingMode.Local 'Get the data for the report Dim myData As IEnumerable(Of ReportOrderDetail) = LoadDataOrderByID(myOrder.orderID) Select Case strReportName Case "Invoice" myReportViewer.LocalReport.ReportPath = Server.MapPath("~/Reports/Invoice.rdlc") myReportViewer.LocalReport.DisplayName = myData.FirstOrDefault.orderFactuurID Case "Shipping" myReportViewer.LocalReport.ReportPath = Server.MapPath("~/Reports/Shipping.rdlc") myReportViewer.LocalReport.DisplayName = myData.FirstOrDefault.orderFactuurID & "A" End Select

    myReportViewer.LocalReport.DataSources.Add(New ReportDataSource("OrderDetailOrderDataset", myData)) myReportViewer.Visible = False deviceInfo = "< deviceInfo ><SimplePageHeaders>True</SimplePageHeaders></DeviceInfo>" reportBytes = myReportViewer.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings) Return reportBytes End Function

            Private Function LoadDataOrderByID(intOrderID As Int32) As IEnumerable(Of ReportOrderDetail)

                Using serverContext = ServerApplicationContext.Current
                    ' Get the data from LightSwitch WCF RIA Service
                    Return From invoice In serverContext.DataWorkspace.AggregationData.GetOrderDetailsByOrderReport(intOrderID).Execute()
                           Select invoice

                End Using
            End Function

    The creation of the pdf goes well, a valid object of type Byte() is returned by function 'UploadReportOrder'. Saving this as file confirms that a valid pdf is created, including correct data. But then the creation of the new database record in Table 'FileMetaDatas' fails.

    The code for creating the FileMetaDatas runs fine if I don't create the pdf (comment the two lines calling the function 'UploadReportOrder'), thus meaning that something happens during the creation of the pdf that 'upsets' the execution of the second part of the invoke, the creation and saving of the FileMetaData record.

    I have tried to read/understand a lot about threading in Lightswitch (gives me comfort to see many people struggle with the same problems), have tried many solutions, but none works.

    Hope someone can help, thanks a lot in advance!

    Wednesday, September 23, 2015 1:35 PM

All replies

  • Can you show the error log? it's really tough issue.
    Thursday, September 24, 2015 9:03 AM