Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting System.NotSupportedException on Azure App Service #80

Open
Dre-Tas opened this issue Dec 8, 2023 · 2 comments
Open

Getting System.NotSupportedException on Azure App Service #80

Dre-Tas opened this issue Dec 8, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@Dre-Tas
Copy link

Dre-Tas commented Dec 8, 2023

Bug report

Describe the bug

In supabase have a Recipes table with 2 columns: id (int8 and primary key) and Name (text)

So in my C# code I have a Recipe class:

using Postgrest.Attributes;
using Postgrest.Models;

namespace Planner.Classes
{
    [Table("Recipes")]
    public class Recipe : BaseModel
    {
        [PrimaryKey("id")]
        public int Id { get; set; }
        [Column("Name")]
        public string Name { get; set; }
        [Reference(typeof(Ingredient))]
        public List<Ingredient> Ingredients { get; set; }
    }
}

where Ingredients is coming from a many-to-many join table (which I believe shouldn't be important now...I think)

In a separate project in the same solution I have created a .NET Core 6.0 API with a controller.
To make it extremely simple right now for reporting, the controller is this:

using Microsoft.AspNetCore.Mvc;
using WeeklyMealPlanner.Classes;

namespace PlannerApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class RecipesController : ControllerBase
    {
        [HttpGet(Name = "GetRecipes")]
        public List<Recipe> GetRecipes()
        {
            return new List<Recipe> { new Recipe { Name = "A" } };
        }
    }
}

When I test this locally with a console app, everything works normally and I get a list of all my recipes stored in supabase, their ingredients, etc.
But when I publish this to Azure App Service then I get a 500 error in the browser and if I check Application Insights I see that it's reporting a System.NotSupportedException at System.Text.Json.ThrowHelper.ThrowNotSupportedException_DictionaryKeyTypeNotSupported with this message:
"The type 'Postgrest.Attributes.PrimaryKeyAttribute' is not a supported dictionary key using converter of type 'System.Text.Json.Serialization.Converters.SmallObjectWithParameterizedConstructorConverter5[Postgrest.Attributes.PrimaryKeyAttribute,System.String,System.Boolean,System.Object,System.Object]'. Path: $.PrimaryKey. The type 'Postgrest.Attributes.PrimaryKeyAttribute' is not a supported dictionary key using converter of type 'System.Text.Json.Serialization.Converters.SmallObjectWithParameterizedConstructorConverter5[Postgrest.Attributes.PrimaryKeyAttribute,System.String,System.Boolean,System.Object,System.Object]'. "

I have upgraded all packages including supabase-csharp, postgrest-csharp and Newtonsoft.Json

I also tried to modify the Recipe class so that it doesn't use any postgrest or supabase library and my controller returns correctly a recipe of name "A" in Chrome.

namespace WeeklyMealPlanner.Classes
{
    public class Recipe
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<Ingredient> Ingredients { get; set; }
    }
}

I'm really out of ideas right now.

Am I doing something wrong??

PS. Thanks for supabase!!. I just recently discovered it and it awesome!! Keep up the good work!

System information

  • OS: Windows
  • Browser: chrome
@Dre-Tas Dre-Tas added the bug Something isn't working label Dec 8, 2023
@wiverson
Copy link
Collaborator

wiverson commented Dec 8, 2023

Not sure off the cuff, but a few ideas off the top of my head:

  • check to see if the .net versions match
  • check to see if there are any constraints on reflection on the Azure service
  • check to see if there is a tweaked version of NewtonSoft for Azure (on the one hand I'd be surprised, but this is true for Unity)

I also popped your Q into an LLM to see what it would suggest and it came back with these two ideas...

To resolve this issue, you can try the following steps:

Use the [JsonProperty] attribute from Newtonsoft.Json:
Replace the Postgrest-specific attributes with [JsonProperty] attributes from the Newtonsoft.Json library. This will ensure that the JSON serialization process is consistent across different environments.

Here's an example of how you can modify your Recipe class:

using Newtonsoft.Json;

public class Recipe
{
    [JsonProperty("id")]
    public int Id { get; set; }
    [JsonProperty("Name")]
    public string Name { get; set; }
    public List<Ingredient> Ingredients { get; set; }
}

Check JSON Serialization Settings:
Make sure that your JSON serialization settings are consistent between your local environment and Azure App Service. You can configure JSON serialization settings in your Startup.cs or wherever you configure your JSON serialization. Ensure that you are using Newtonsoft.Json as the JSON serializer and that the settings are the same in both places.

// In Startup.cs or your configuration file
services.AddControllers().AddNewtonsoftJson(options =>
{
    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // This may be needed for your relationships.
});

@Dre-Tas
Copy link
Author

Dre-Tas commented Dec 10, 2023

Legend!! Thanks @wiverson !

I didn't try using the [JsonProperty] attribute as I first tried adding the Newtonsoft options to Program.cs and that did the trick (also adding the Microsoft.AspNetCore.Mvc.NewtonsoftJson) as required in order to have the AddNewtonsoftJson method).

Would you be able to explain why that fixed it though? I'd be curious to understand.

Thank you so much again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants