public async Task SendReportPdfAsync( [QueueTrigger("sendreportpdf")] ReportQueue reportMessage, [Blob("report/{ReportId}.pdf", FileAccess.Write)] CloudBlockBlob reportPdfOutput, #if DEBUG [Blob("report/{ReportId}.html", FileAccess.Write)] CloudBlockBlob reportHtmlOutput, #endif CancellationToken cancellationToken) { try { Trace.CorrelationManager.ActivityId = Guid.NewGuid(); logger.LogTrace($"SendReport trigger event recived, ID: {reportMessage?.ReportId}."); if (string.IsNullOrEmpty(reportMessage?.CultureName)) { throw new ArgumentNullException(nameof(ReportQueue.CultureName)); } CultureHandler.SetCulture(reportMessage.CultureName); var translate = new Translate(); var report = dbContext.Reports.Where(r => (r.Status == ReportStatus.Created || r.Status == ReportStatus.Resending) && r.PartitionId == reportMessage.PartitionId && r.Id == reportMessage.ReportId).FirstOrDefault(); if (report == null) { throw new Exception("Report do not exists or has invalid status and is therefore not send."); } var reportData = await report.ReportData.FromJson <ReportDataApi>(); var organization = dbContext.Organizations.Where(o => o.Id == reportMessage.PartitionId).Select(o => new { o.Name, o.Address, o.Logo }).FirstOrDefault(); using (var pdfReportHtmlStream = await ReportHtml.CreateReportStream(translate, reportData.ShowGroupColl, organization?.Logo, organization?.Name, organization?.Address, reportData.ReportTitle, reportData.ReportSubTitle, reportData.ReportText, reportData.Report)) { #if DEBUG await reportHtmlOutput.UploadFromStreamAsync(pdfReportHtmlStream); pdfReportHtmlStream.Position = 0; #endif using (var reportPdfStream = new MemoryStream()) { logger.LogTrace($"Before create PDF: {reportMessage?.ReportId}."); PdfProvider.CreatePdfAsync(reportPdfStream, pdfReportHtmlStream, cancellationToken: cancellationToken); logger.LogTrace($"After create PDF: {reportMessage?.ReportId}."); if (!cancellationToken.IsCancellationRequested) { await SendEmailReport(reportPdfStream, translate, report); await UpdateReportStatus(reportMessage.PartitionId, reportMessage.ReportId, report.Status == ReportStatus.Created?ReportStatus.Send : ReportStatus.Resend); } await reportPdfOutput.UploadFromStreamAsync(reportPdfStream); } } logger.LogTrace($"SendReport report send, ID: {reportMessage?.ReportId}."); } catch (Exception ex) { logger.LogError(ex, $"SendReport failed, ID: {reportMessage?.ReportId}."); throw; } }
protected void Application_BeginRequest(object sender, EventArgs _args) { _cultureHandler.SetCulture(HttpContext.Current); }
public async Task SendInvoicePdfAsync( [QueueTrigger("sendinvoicepdf")] InvoiceQueue invoiceMessage, [Blob("invoice/{InvoiceId}.pdf", FileAccess.Write)] CloudBlockBlob invoicePdfOutput, #if DEBUG [Blob("invoice/{InvoiceId}.html", FileAccess.Write)] CloudBlockBlob invoiceHtmlOutput, #endif CancellationToken cancellationToken) { try { Trace.CorrelationManager.ActivityId = Guid.NewGuid(); logger.LogTrace($"SendInvoice trigger event recived, ID: {invoiceMessage?.InvoiceId}."); if (string.IsNullOrEmpty(invoiceMessage?.CultureName)) { throw new ArgumentNullException(nameof(InvoiceQueue.CultureName)); } CultureHandler.SetCulture(invoiceMessage.CultureName); var translate = new Translate(); var invoice = dbContext.Invoices.Where(i => (i.Status == InvoiceStatus.Created || i.Status == InvoiceStatus.Resending) && i.PartitionId == invoiceMessage.PartitionId && i.Id == invoiceMessage.InvoiceId).FirstOrDefault(); if (invoice == null) { throw new Exception("Invoice do not exists or have invalid status and is therefore not send."); } var invoiceData = await invoice.InvoiceData.FromJson <InvoiceDataApi>(); var organization = dbContext.Organizations.Where(o => o.Id == invoiceMessage.PartitionId).Select(o => new { o.Name, o.Address, o.Logo, o.VatNumber }).FirstOrDefault(); using (var pdfInvoiceHtmlStream = await InvoiceHtml.CreateInvoiceStream(translate, invoice, invoiceData, organization?.Logo, organization?.Name, organization?.Address)) { #if DEBUG await invoiceHtmlOutput.UploadFromStreamAsync(pdfInvoiceHtmlStream); pdfInvoiceHtmlStream.Position = 0; #endif using (var invoicePdfStream = new MemoryStream()) { logger.LogTrace($"Before create PDF: {invoiceMessage?.InvoiceId}."); PdfProvider.CreatePdfAsync(invoicePdfStream, pdfInvoiceHtmlStream, cancellationToken: cancellationToken); logger.LogTrace($"After create PDF: {invoiceMessage?.InvoiceId}."); if (!cancellationToken.IsCancellationRequested) { await SendEmailInvoice(invoicePdfStream, translate, invoice); await UpdateInvoiceStatus(invoiceMessage.PartitionId, invoiceMessage.InvoiceId, invoice.Status == InvoiceStatus.Created?InvoiceStatus.Send : InvoiceStatus.Resend); } await invoicePdfOutput.UploadFromStreamAsync(invoicePdfStream); } } logger.LogTrace($"SendInvoice invoice send, ID: {invoiceMessage?.InvoiceId}."); } catch (Exception ex) { logger.LogError(ex, $"SendInvoice failed, ID: {invoiceMessage?.InvoiceId}."); throw; } }