Search code examples
c#visual-studioservice-reference

Where is "System.PlatformNotSupportedException: Dynamic code generation is not supported on this platform" coming from?


I've added a service reference in Visual Studio 2022 Community (C#), and I'm using the service reference's methods below. This is a console application.

1 ServRef1.Service1Client client1 = new ServRef1.Service1Client();
2 ServRef1.HTDesigner hd = await client1.GetHTDesignerAsync();

Line 2 is throwing the error I copied into the title here about Dynamic code generation not being supported. My question is:

  • Is this being generated by the service I'm referencing, or is it something I'm doing wrong?

I'm relatively new to Visual Studio settings, and I'm wondering if some compiling option I've selected has done this. I remember there was an option to pre-compile code, so maybe that's what's causing it? I can't seem to find where that is to turn it off, however.

On the other hand, I'm not sure the service reference code is great. Could that be causing the issue?

I'm using .NET 8.0.

As far as a minimal, reproducible example, I'm not sure I can do that because I'd have to provide information about the service reference, which is private. However, I can say that the lines I show here are literally the first two lines of the program, it has nothing else going on. I created a console application, added the service reference, created these two lines based on documentation, then ran into the bug.

Thanks!


Solution

  • My application was set to compile as Native AOT, which apparently is not compatible with using a Service Reference because the Service Reference generates dynamic code.