To demonstrate the issue, I've added a few POST methods to the solution created by the Blazor WASM solution template. Some work, some don't, generating errors which I don't understand and I'm hoping someone out there can educate me.
Here's the client side code:
protected override async Task OnInitializedAsync()
{
try
{
// From the Blazor template
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
var w = new WeatherForecast();
// Works
await Http.PostAsJsonAsync<WeatherForecast>("WeatherForecast", w);
MemoryStream s = new MemoryStream();
// Generates exception: 'Timeouts are not supported on this stream.'
await Http.PostAsJsonAsync<MemoryStream>($"WeatherForecast/stream", s);
List<MemoryStream> l = new List<MemoryStream>() { new MemoryStream() };
// Generates exception: 'Timeouts are not supported on this stream.'
await Http.PostAsJsonAsync<List<MemoryStream>>($"WeatherForecast/list", l);
List<MemoryStream> m = new List<MemoryStream>();
// Works
await Http.PostAsJsonAsync<List<MemoryStream>>($"WeatherForecast/list", m);
// PdfDocument is part of the PdfSharp library which I'm using to build pdf
// files but I need to send it into the API to save it on a server.
PdfDocument doc = new PdfDocument();
// Generates exception: 'System.Security.Cryptography.Algorithms is not
// supported on this platform.'
await Http.PostAsJsonAsync<PdfDocument>("WeatherForecas/pdf", doc);
}
catch (Exception e)
{
throw;
}
}
Here's the server side API code:
[HttpPost]
public void Post([FromBody] WeatherForecast w)
{
if (w == null) return;
}
[HttpPost("stream")]
public void Post([FromBody] MemoryStream s)
{
if (s == null) return;
}
[HttpPost("list")]
public void Post([FromBody] List<MemoryStream> s)
{
if (s == null) return;
}
[HttpPost("pdf")]
public void Post([FromBody] PdfDocument doc)
{
if (doc == null) return;
}
}
The comments show which calls work and which ones don't, and for the ones that don't, the errors don't make sense to me. Can anyone help me understand why I can't send a PdfDocument to an API?
Thanks in advance for your help.
PdfDocumentdepends onSystem.Security.Cryptography.Algorithmswhich is not supported in browser-wasm.PdfDocumentas json, you should generate the pdf (using some client side tools) and then send it as a file.