0

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.

2
  • 1
    Guessing because we don't have enough info - PdfDocument depends on System.Security.Cryptography.Algorithms which is not supported in browser-wasm. Commented Aug 26, 2022 at 23:16
  • 1
    1) I'm not sure if client side pdf generation with this library is supported (based on the error - no) 2) even if it was - you should not send instance of PdfDocument as json, you should generate the pdf (using some client side tools) and then send it as a file. Commented Aug 27, 2022 at 19:16

1 Answer 1

1

Generates exception: 'System.Security.Cryptography.Algorithms is not supported on this platform.'

This is a known issue: System.Security.Cryptography APIs not supported on Blazor WebAssembly. And there are no good workarounds to suggest at this time.

So, as a workaround, you could try to read the pdf content and send the content as the string type, or send the pdf file to the API method.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. I turned it into a byte array and that works however I'd still like to learn/understand why I can't send an instance of an object to an API via json serialization. Even if PdfDocument is dependent on System.Security.Cryptography which isn't supported in wasm, I'm not doing anything with that so is the issue in the json serialization or somewhere else? And what about the error sending a MemoryStream? Is that a similar issue? I'd love to understand which objects I can send to an API and which ones I can't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.