最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Issues implementing .NET 9 HybridCache in ASP.NET Core 9 Web API controllers - Stack Overflow

programmeradmin4浏览0评论

For a few days now I'm trying to implement .NET 9 hybridcache in my ASP.NET Core Web API project, but so far, I'm totally failing.

A simple controller I tried to "migrate":

[MapToApiVersion("1.0")]
[HttpGet]
[EnableQuery]
public async Task<ActionResult<IEnumerable<Dto.Game>>> GetGameV1()
{
     var games = await dbcontext.Game
                                .Include(st => st.GameGameday)
                                .AsNoTracking().ToListAsync();
    
     if (games != null)
     {
          return mapper.Map<List<Dto.Game>>(games);
     }
     else
     { 
          return NotFound();
     }
 }

This has been migrated to:

[MapToApiVersion("1.0")]
[HttpGet]
[EnableQuery]
public async Task<ActionResult<IEnumerable<Dto.Game>>> GetGameV1(CancellationToken ct = default)
{
     var games = await cache.GetOrCreateAsync(
                          $"games", // Unique key to the cache entry
                          async cancel =>
                          {
                               return await dbcontext.Game
                                        .Include(st => st.GameGameday)
                                        .AsNoTracking().ToListAsync();
                          },
                          cancellationToken: ct
     );

     if (games != null)
     {
         return mapper.Map<List<Dto.Game>>(games);
     }
     else
     { 
         return NotFound();
     }
}

When the migrated controller is called it throws an exception:

Exception caught in ExceptionHandlingMiddleware of localhost: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported.

My class for Game looks like this:

public partial class Game
{
    [JsonPropertyName("id")]
    public long Id { get; set; }

    [JsonPropertyName("gameday")]
    public GameDay? GameDay { get; set; }

    [JsonPropertyName("number")]
    public uint Number { get; set; }

    [JsonPropertyName("gameresults")]
    public List<GameResults>? GameResults { get; set; }

    public Game()
    {}
}

Does anyone have an idea what could cause this issue?

This is driving me crazy!

Thank you.

Requested classes:

public partial class GameResults
{
    [Key]
    public long GameResultId { get; set; }

    public Game GameResultGame { get; set; }

    public uint GameResultRank { get; set; }

    public Player? GameResultPlayer { get; set; }
}

public partial class GameDay
{
    [Key]
    public long GameDayId { get; set; }

    public DateOnly GameDayDate { get; set; }

    public string? GameDayLocation { get; set; } 

    public List<Game>? GameDayGames { get; set; } 
}

HybridCache related part of Program.cs:

    #pragma warning disable EXTEXP0018
    builder.Services.AddHybridCache(options =>
    {
        options.MaximumPayloadBytes = 1024 * 1024;
        options.MaximumKeyLength = 1024;
        options.DefaultEntryOptions = new HybridCacheEntryOptions
        {
            Expiration = TimeSpan.FromMinutes(60),
            LocalCacheExpiration = TimeSpan.FromMinutes(60)
        };
    });
    #pragma warning restore EXTEXP0018
发布评论

评论列表(0)

  1. 暂无评论