Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to "marshal"/"force" code to run on the server side?

$
0
0

Context

I'm trying to implement a button that generates, and then downloads, a PDF. The method I'm calling to do so looks something like this:

private async void ExportToPdf(){    string fileName = $"{_entity.Name}.pdf";    string filePath = Path.Join(AppDomain.Current.BaseDirectory, "PdfBuffer", fileName);    await _pdfBuilder.GeneratePdf(_entity, printPath);    await _fileDownloadService.DownloadAsync(fileName, filePath);    File.Delete(filePath);}

Some key points:

  • _pdfBuilder is a custom PDF builder. While not strictly relevant to the question, I'm sure someone will point this out: I am not in control of this code. I have to save to a file because the public API does not allow me to just get the bytes.
  • _fileDownloadService is a scoped service (requires an IJSRuntime), adapted from this blogpost.

Problem

The problem lies in the fact that _pdfBuilder.GeneratePdf(...)must run on the server. The client is incapable of the work required, so awaiting the method just hangs indefinitely and never completes. The file download service, then, must be able to stream the PDF file's bytes down to the client. As blazor somewhat blurs the line between client and server, I'm not sure how to go about making this happen.

The question, therefore, is: How can I explicitly tell _pdfBuilder.GeneratePdf(...) to run on the server?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>