Home

Roslyn Microsoft.CodeAnalysis.CSharp.Scripting

I've been trying to play around with Roslyn scripting using LinqPad but have had no luck. I find that with LinqPad 5.08.01 the full Microsoft.CodeAnalysis.CSharp.Scripting NuGet package doesn't seem be loaded (the Microsoft.CodeAnalysis.CSharp.Scripting.dll assembly is missing).

With the Beta 5.09.01 I'm able to reference everything but evaluating expressions with CSharpScript throws exceptions.

For example, the following program throws
MissingMethodException: Method not found: 'System.Collections.Immutable.ImmutableArray`1 System.Reflection.Metadata.MetadataReader.GetBlobContent(System.Reflection.Metadata.BlobHandle)'
I've pulled in Microsoft.CodeAnalysis.CSharp.Scripting 1.3.2 and have included the Microsoft.CodeAnalysis.CSharp.Scripting namespace. The NuGet package also pulls in System.Collections.Immutable and System.Reflection.Metadata referred to in the exception.

The same sample running in a Console application works fine. Any ideas what's missing? Thanks.
void Main()
{
	int result = 0;
	var globals = new Globals { X = 80, Y = 10 };
	CSharpScript.EvaluateAsync<int>("X+Y", null, globals).ContinueWith(s => result = s.Result).Wait();
}

public class Globals
{
	public int X;
	public int Y;
}

Comments

  • edited August 2016
    This is a known problem: a dynamic binding redirect fails because the old version of System.Immutable is in the GAC. Right now, there are two workarounds.

    The first is to use the prerelease version of Microsoft.CodeAnalysis.CSharp.Scripting, which doesn't reference a version of CodeAnalysis that's likely to be in the GAC.

    The second is to manually specify binding redirects, which you can copy from the app.config file that VS creates when you add the NuGet package.

    Go to Query | App.Config, and paste the following in:
    <configuration>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Reflection.Metadata" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-1.2.0.0" newVersion="1.2.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.IO.FileSystem" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Diagnostics.StackTrace" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.IO.FileSystem.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>
    I'll let you know when there's an automatic fix for this.
  • edited September 2016
    Update: the 5.09.02 LINQPad beta fixes this automatically:
    https://www.linqpad.net/Download.aspx#beta

    LINQPad now identifies the problem scenario and creates binding redirects on the fly.
Sign In or Register to comment.