Skip to content

Cannot use CSharpScript API with PublishSingleSingle option #50719

@CyberSinh

Description

@CyberSinh

Hi,

I use the following code to run C# scripts in my software. Everything is fine under .NET 5.0 when I publish the software without PublishSingleSingle option.

ScriptOptions options = ScriptOptions.Default
   .WithMetadataResolver(ScriptMetadataResolver.Default.WithSearchPaths(new[] { RuntimeEnvironment.GetRuntimeDirectory(), AppInfo.StartupFolder }))
   .WithSourceResolver(new SourceFileResolver(ImmutableArray<string>.Empty, AppInfo.ScriptsFolder))
   .AddReferences(Assembly.GetExecutingAssembly()); // Assembly.Location returns "" with PublishSingleFile option, but using Environment.GetCommandLineArgs()[0] instead Assembly.GetExecutingAssembly() doesn't work too
   
Script script = CSharpScript.Create(code, options, typeof(GlobalVariables));
//Script script = CSharpScript.Create(code, null, null); // using CSharpScript with default parameter doesn't work too
script.Compile();

But when I publish my software with the PublishSingleFile option, the code doesn't work anymore and I get the error:
"can't create a metadata reference to an assembly without location".

Is there is a way to reference my main assembly and/or .NET 5.0 system assemblies embedded in the main executable?
Is there any code sample to use the CSharpScript API when PublishSingleFile is enabled?

Thanks for your help.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions