Open CyberSinh opened 3 years ago
cc @tmat
Simple reproduction, with .NET 5 and PublishSingleFile
set to true.
await CSharpScript.EvaluateAsync(@"""Hello World!""");
The issue seems to be from Script.GetReferencesForCompilation
:
var corLib = MetadataReference.CreateFromAssemblyInternal(typeof(object).GetTypeInfo().Assembly);
Maybe this workaround could be helpful for single-file publish?
public static MetadataReference GetRawMetadataReference(this Type type)
{
unsafe
{
return type.Assembly.TryGetRawMetadata(out var blob, out var length)
? AssemblyMetadata
.Create(ModuleMetadata.CreateFromMetadata((IntPtr) blob, length))
.GetReference()
: throw new InvalidOperationException($"Could not get raw metadata for type {type}");
}
}
Same issue here, don't know how to fix it 😿
@andersstorhaug have you found any reliable fix for this issue?
@BlackOfWorld I haven't found a workaround for this yet, though, this StackOverflow post may be of some use.
I think it could be solved by memory patching IL code, but I find it very hacky workaround.
Here's an example that I was able to get working by effectively (and naively) reproducing the CSharpScript
API with the aforementioned mscorlib
MetadataReference
workaround.
This just bit me in the tail feather today using .NET 6.0 RTM (6.0.100). Any update on whether this will be addressed in a future release? The workaround did not work for me when adding the assemblies to ScriptOption.Default.AddReferences() and supplying that ScriptOptions instance to CSharpScript.EvaluateAsync
this issue still happens anyone know a fix? (only when i publish single file)
Same issue, still happens.
One of CS-Script users pointed to this thread.
CS-Script recently incorporated the existing workaround for this issue so Roslyn users can use this option fo single-file published applications:
var calc = CSScript.Evaluator
.Eval(@"using System;
public class Script
{
public int Sum(int a, int b)
{
return a+b;
}
}
return new Script();");
int sum = calc.Sum(1, 2);
Console.WriteLine(sum);
The complete sample can be found here.
I have a fix for this but it requires a couple of small changes to roslyn.
I have a fix for this but it requires a couple of small changes to roslyn.
Can you share
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.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 whenPublishSingleFile
is enabled?Thanks for your help.