0

I'm trying to add System.Text.Json source generation to one of my projects, a .net6 rest api.

When I try to specify the partial class according to instructions I get this behavior:

vs thinks this won't build properly

But I can still build the project, but no files are source generated.

If I just start a new project, using the ASP.NET Core Web API template and add the exact same code it works and inspecting the source generation analyser shows the files being generated. I tried looking through solution and project settings to figure out what could be causing this, but I don't see why this would happen.

The project I'm trying to do this for used to be a .netcore3.1 project, but it has been migrated to .net6, but maybe there's something there that prevents this project from using source generation?

Here's the classes for reference, not that there's much in them:

namespace Test
{
    public class TestClass
    {
        public int MyNumber { get; set; }
        public Guid MyGuid { get; set; }
    }
}

namespace Test
{
    [JsonSerializable(typeof(TestClass))]
    internal partial class TestContext : JsonSerializerContext
    {
    }
}
  • The fact that VS has highlighted the `JsonSerializable` and `JsonSerializerContext` in white suggests that you're missing the right `using` statements? – canton7 Apr 13 '22 at 14:36
  • Have you tried adding the constructor the error is moaning about? – DubDub Apr 13 '22 at 14:40
  • @DubDub You don't want to do that: the SG generates it – canton7 Apr 13 '22 at 14:40
  • I just tried your code and it works fine. Make sure you have `using System.Text.Json.Serialization;` – canton7 Apr 13 '22 at 14:41
  • It would also be good to see what the warning on the `Test` is, and any other errors/warnings you have – canton7 Apr 13 '22 at 14:43
  • Try opening the two project files in a text editor and comparing them? BTW, please [include the compilation error message in your question as text](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557). – sbridewell Apr 13 '22 at 17:02
  • The warning is visible in the attached picture. I have the right using statements. The exact same code does work if I create a new project, so I'm more wondering if anyone has a clue what outside the classes themselves could cause this behaviour? I'm thinking something in the .csproj or .sln files? Maybe something in my VS settings? – Carl Granström Apr 13 '22 at 21:43
  • @CarlGranström I meant the warning on `namespace Test`, which isn't shown in your picture. Maybe there's something in your csproj causing this, but we can't tell unless you share it. There's also a known issue where SGs stop generating sources if they hit an exception until you restart VS, which is why I asked to see any other errors/warnings you have – canton7 Apr 14 '22 at 07:44
  • I'm seeing exactly this same behavior in Rider. It builds and runs just fine, complains about the internal partial class. – codewise Dec 05 '22 at 18:20

0 Answers0