0

In my dataservice (client-side) I try to call controller API with this (the Id 36fc1b86-9697-49c5-978c-825f465de73b is hard coded to show the problem) :

return await JsonSerializer.DeserializeAsync<IEnumerable<Stage>>
(
    await _httpClient.GetStreamAsync($"api/stage/36fc1b86-9697-49c5-978c-825f465de73b"), 
    new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }
 );

Server-side (API)

[HttpGet("{id}")]
public IActionResult GetAllStage(string id)
{
    return Ok(_stageRepository.GetAllStagesById(id));
}

And the error :

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Value cannot be null. (Parameter 'source') System.ArgumentNullException: Value cannot be null. (Parameter 'source')

But if GUID starts with a letter (see below /a...) everything works #1 like this :

return await JsonSerializer.DeserializeAsync<IEnumerable<Stage>>
(
    await _httpClient.GetStreamAsync($"api/stage/a0bdb087-dadc-4382-9a28-75f93917698b"), 
    new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });

Workaround. I put a extra letter (here X) at the very begining :

return await JsonSerializer.DeserializeAsync<IEnumerable<Stage>>
    (await _httpClient.GetStreamAsync($"api/stage/X{id}"), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });

And remove it in the API...

    [HttpGet("{id}")]
    public IActionResult GetAllStage(string id)
    {
        return Ok(_stageRepository.GetAllStagesById(id.Remove(0, 1)));
    }

Any idea about this bug?

3
  • I've tried but it works without any error on a new Blazor WASM project hosted Commented Sep 15, 2021 at 14:05
  • Don't write code like this. You gain nothing at all by trying to stuff everything in a single line. You can't read or debug your code any more, making it very easy to introduce bugs in the code and very hard to troubleshoot it. What happens if an API request fails? You'll have no chance to handle it. Commented Sep 15, 2021 at 14:28
  • As for bugs, look into the code after cleaning it up. Somehow, somewhere you passed a null to a method. You didn't post the full exception text so one can only guess where the problem may be, or what method was called. Quite likely the problem has nothing to do with the question's code Commented Sep 15, 2021 at 14:31

1 Answer 1

3

The clue is in

Unhandled exception rendering component: Value cannot be null. (Parameter 'source')

This means an IEnumerable is null where it shouldn't be. Since this is thrown from WebAssemblyRenderer you probably use the results in a @foreach() { <tag> </tag> } without a null guard.

And why does it depend on the starting char of the Guid? That has to be a problem inside GetAllStagesById().
We can't debug code that is not included.

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

Comments

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.