dotnet / dotnet/roslyn

Scripting API - Operation is not supported on this platform. (when AOT published)

Open
#73,689 0 comments 0 reactions 1 assignee Claimed by @tmat View on GitHub
Area-Interactive
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

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 net8 console application (puplish AOT profile) with basic project setup:

```xml


Exe
net8.0
enable
enable
true
true



```

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

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

then the simple main task:

```csharp
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 params:
```
Configuration: Release
Deployment mode: Self-contained
Target runtime: win-x64
```

and running the application it throws an exeption:

`Operation is not supported on this platform.`

**Diagnostic**:

I identified by using outputs that the problem is with:

`CSharpScript.EvaluateAsync(codeToEval, options);`

Is there any workaround or a best practise for this?
Thx guys

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.