System.NotSupportedException: Serialization and deserialization of 'System.Action' instances are not supported. Path: $.MoveNextAction
Asked Answered
M

4

23

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.

Myrtia answered 25/11, 2022 at 14:35 Comment(2)
RpgClass is an enumMyrtia
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 asyncRuble
M
76

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

Myrtia answered 25/11, 2022 at 16:12 Comment(1)
yup that works for meSubhead
D
1
[HttpGet("Info")]
[Authorize]
public async Task<ActionResult<UserDto>> GetCurrentUserInfo()
{
    var user = await _userService.GetCurrentUserInfo();
    return user.ToDto();
}

please remove ok and return directly

Danie answered 28/3, 2023 at 11:2 Comment(0)
V
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());
}
Vitalize answered 6/3, 2023 at 16:11 Comment(2)
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 problemVitalize
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 How 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.W
D
0

Had a similar problem, mistakenly I passed () => someMethod instead of someMethod as my MapGet handler.

Devitrify answered 27/5, 2024 at 20:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.