ekolis
ekolis

Reputation: 6806

Roslyn crashes when I try to run a script

I have this Program class:

using Microsoft.CodeAnalysis.CSharp.Scripting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RoslynExperiment
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var result = ComputeAsync("1+2");
            Console.WriteLine(result.Result);
        }

        private static async Task<int> ComputeAsync(string formula)
        {
            return await CSharpScript.EvaluateAsync<int>(formula);
        }
    }
}

But when I call ComputeAsync, I get this error:

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

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

Why would I be getting this exception when I try to execute my script? How can I fix it?

Upvotes: 1

Views: 286

Answers (1)

jamesfdearborn
jamesfdearborn

Reputation: 799

This is a documented issue with Visual Studio and .Net. Try changing your target framework to netstandard2.0.

Or, you can try adding the following to your .csproj:

<ItemGroup>
  <PackageReference Include="Legacy2CPSWorkaround" Version="1.0.0">
    <PrivateAssets>All</PrivateAssets>
  </PackageReference>
</ItemGroup>

Upvotes: 1

Related Questions