12

I am following a simple tutorial on .NET 6 and it should work really simple, but apparently I get the exception. The sample code is the following:

public async Task<ServiceResponse<List<GetCharacterDto>>> GetAllCharacters()
{
    var response = new ServiceResponse<List<GetCharacterDto>>();
    var dbCharacters = await _context.Characters.ToListAsync();
    response.Data = dbCharacters.Select(c => _mapper.Map<GetCharacterDto>(c)).ToList();

    return response;
}

The code in GetCharacterDto is:

public class GetCharacterDto
{ 
    public int Id { get; set; }
    
    public string Name { get; set; } = "Frodo";

    public int HitPoints { get; set; } = 100;

    public int Strength { get; set; } = 10;

    public int Defense { get; set; } = 10;

    public int Intelligence { get; set; } = 10;

    public RpgClass Class { get; set; } = RpgClass.Knight;
}

RpgClass:

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum RpgClass
{
    Knight = 1,
    Mage = 2,
    Cleric = 3
}

The exception

System.NotSupportedException: Serialization and deserialization of 'System.Action' instances are not supported. Path: $.MoveNextAction. Is thrown right at

var dbCharacters = await _context.Characters.ToListAsync(); 

If I call it synchronously

_context.Characters.ToList();

it works alright, but can't get it to work asynchronously.

I have both .NET 5 SDK and .NET 6 SDK installed, if that could be a potential issue.

Twenty
  • 5,234
  • 4
  • 32
  • 67
vpp090
  • 603
  • 1
  • 5
  • 9
  • RpgClass is an enum – vpp090 Nov 25 '22 at 14:48
  • 4
    the problem is not here ... you prolly have in controller `results = GetAllCharacters(); return results;` it would work with sync but will have this symptomps for async – Selvin Nov 25 '22 at 15:06
  • @Selvin -- genius. i had the same problem and somehow intellisense wasn't giving me the error but this was the key! – gotmike Feb 08 '23 at 01:49

3 Answers3

45

I was missing an await in the controller where I was calling the GetAllCharacters() method.

vpp090
  • 603
  • 1
  • 5
  • 9
1
[HttpGet("Info")]
[Authorize]
public async Task<ActionResult<UserDto>> GetCurrentUserInfo()
{
    var user = await _userService.GetCurrentUserInfo();
    return user.ToDto();
}

please remove ok and return directly

0

I found this problem when I had a task in a service without arguments, and I was trying to call it. When I added and argument it worked, but it's just a work-around. I think there's a better way to do it but I don't know what to do.

[HttpGet("Info")]
[Authorize]
public async Task<ActionResult<UserDto>> GetCurrentUserInfo()
{
    var user = await _userService.GetCurrentUserInfo();
    return Ok(user.ToDto());
}

I just added an argument and it worked

[HttpGet("Info")]
[Authorize]
public async Task<ActionResult<UserDto>> GetCurrentUserInfo()
{
    var user = await _userService.GetCurrentUserInfo("0");
    return Ok(user.ToDto());
}

If there's a better way please tell me Much appreciated

Twenty
  • 5,234
  • 4
  • 32
  • 67
Omar
  • 1
  • I just realized that the method in the user Service and the controller method has the same name and when both method had the same signature that caused the : System.NotSupportedException: Serialization and deserialization of 'System.Action' instances are not supported. Path: $.MoveNextAction Just change the method name and it will be fixed if you have the same problem – Omar Mar 06 '23 at 16:18
  • 3
    Please [edit], move the info from your comment into your answer and make sure that the whole thing is an answer to the question at the top of this page and according to [answer]. And not, e.g. your experience with a similar but different problem and how you found a solution to that. Because that is the impression your post and comment currently give. – Yunnosch Mar 06 '23 at 18:58