Roslyn, Could not load file or assembly System.Runtime

Viewed 582

I am new with Roslyn and I am trying to compile my first code at runtime.

The code compile(exe) without error but when i run it throught process.Start() in the concole output appers the error

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

I read that many other people had same problem but the solution here : https://github.com/dotnet/core/issues/2082 not worked for me

This is the full code of my console app :

using System;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using System.IO;
using System.Diagnostics;
using Microsoft.Extensions.DependencyModel;
using System.Linq;

namespace TestRoslyn
{
    class Program
    {

        static string code = @"
                                using System;
                                namespace First
                                {
                                    public class Program
                                    {
                                        public static void Main()
                                        {" +
                                            "Console.WriteLine(\"Hello, world!\");"
                                    + @"
                                        }
                                    }
                                }
                                ";

        static void Main(string[] args)
        {
            MetadataReference[] _ref = DependencyContext.Default.CompileLibraries
                           .SelectMany(cl => cl.ResolveReferencePaths())
                           .Select(asm => MetadataReference.CreateFromFile(asm))
                           .ToArray();

            var syntaxTree = SyntaxFactory.ParseSyntaxTree(SourceText.From(code));
            var assemblyPath = Path.ChangeExtension(Path.GetTempFileName(), "exe");
            var compilation = CSharpCompilation.Create(Path.GetFileName(assemblyPath))
                .WithOptions(new CSharpCompilationOptions(OutputKind.ConsoleApplication))
                .AddReferences(_ref)
                .AddSyntaxTrees(syntaxTree);

            var result = compilation.Emit(assemblyPath);
            if (result.Success) // Compilation is Success
            {
                using (Process process = new Process())
                {
                    process.StartInfo.FileName = assemblyPath;
                    process.Start(); // Here the error appears in the console window
                }
            }
            else
            {
                System.Diagnostics.Debug.Write(string.Join(
                    Environment.NewLine,
                    result.Diagnostics.Select(diagnostic => diagnostic.ToString())
                ));
            }
        }
    }
}

project file

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <PreserveCompilationContext>true</PreserveCompilationContext>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyModel" Version="5.0.0" />
  </ItemGroup>

</Project>

Can someone help me ?

0 Answers
Related