3HMonkey
3HMonkey

Reputation: 137

Operation is not supported in Roslyn (Scripting API) when published AOT

The goal is to create a simple test application that enables the loading of a script and accesses a predefined static class and outputs a value from this class to the console.

Version Used: 4.10.0-3.final

Steps to Reproduce:

I created a new .NET 8 console application (publish AOT profile) with basic project setup:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PublishAot>true</PublishAot>
    <InvariantGlobalization>true</InvariantGlobalization>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.10.0-3.final" />
  </ItemGroup>

</Project>

Added an additional class in RoslynTest.Test namespace to the project:

public static class Foo
{
    public static string Name { get; set; } = "Bar";
}

then the simple main task:

using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;

namespace RoslynTest
{
    internal class Program
    {
        static async Task Main(string[] args)
        {

            try
            {
                var codeToEval =
                @"
                int test = 0;
                var count = test + 15;
                count++;
                Console.WriteLine(count);

                Console.WriteLine(Foo.Name);


                ";

                var options = ScriptOptions.Default;

                options = options
                    .WithImports("System", "RoslynTest.Test")
                    .AddReferences("RoslynTest");

                var result = await CSharpScript.EvaluateAsync(codeToEval, options);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadLine();


        }
    }
}

When running in debug mode it works like expected, so output is:

16
Bar

Expected Behavior:

16
Bar

Actual Behavior: When published using AOT with parameters:

Configuration: Release
Deployment mode: Self-contained
Target runtime: win-x64

and running the application it throws an exception:

Operation is not supported on this platform.

Diagnostic:

I identified by using outputs that the problem is with:

CSharpScript.EvaluateAsync(codeToEval, options);

Is there a workaround or a best practise for this?

Upvotes: 0

Views: 76

Answers (0)

Related Questions