Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception thrown: 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' #358

Closed
davidpfister opened this issue Oct 27, 2022 · 18 comments
Closed
Labels
Area: CSharp Status: Needs Repro This issue needs reproducible code

Comments

@davidpfister
Copy link

Description

I' m trying to use plotly.net in a c# .NETFramework 4.7 desktop application (great job by the way!). For the record I am not using the CSharp binder (only targeting .NET6 from what I have seen since you used record struct).
Everything works fine until I display the chart using .Show(). My console is spammed with: Exception thrown: 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' in Microsoft.CSharp.dll. Looks like the code cannot access the internal dynamic object.

Repro steps

  1. Create a C# project targeting .NETFramework 4.7.1

  2. Reference the Plot;y.Net nuget package version 3.0.1

  3. Create a chart using

LinearAxis xAxis = new();
            xAxis.SetValue("title", "xAxis");
            xAxis.SetValue("zerolinecolor", "#ffff");
            xAxis.SetValue("gridcolor", "#ffff");
            xAxis.SetValue("showline", true);
            xAxis.SetValue("zerolinewidth", 2);

            LinearAxis yAxis = new();
            yAxis.SetValue("title", "yAxis");
            yAxis.SetValue("zerolinecolor", "#ffff");
            yAxis.SetValue("gridcolor", "#ffff");
            yAxis.SetValue("showline", true);
            yAxis.SetValue("zerolinewidth", 2);

            Layout layout = new();
            layout.SetValue("xaxis", xAxis);
            layout.SetValue("yaxis", yAxis);
            layout.SetValue("title", "A Figure Specified by DynamicObj");
            layout.SetValue("plot_bgcolor", "#e5ecf6");
            layout.SetValue("showlegend", true);

            Trace trace = new("line");
            trace.SetValue("x", x);
            trace.SetValue("y", y);


            var fig = GenericChart.Figure.create(ListModule.OfSeq(new[] { trace }), layout);
            GenericChart.fromFigure(fig).Show();
  1. Monitor the output. After some time the plot is eventually displayed, but so many exceptions thrown delay the process.

Expected behavior

No exception should be thrown.

Actual behavior

48 exceptions were thrown in that case

Known workarounds

It is known that dynamic object are internal and thus not accessible. See this thread for a lead.

Related information

  • Windows 10
  • master
@davidpfister
Copy link
Author

Any news? Am I doing something wrong here?

@esond
Copy link

esond commented Feb 1, 2023

+1 on this, getting around 500 exceptions thrown on generation of a column chart in a .NET 7 application.

@kMutagene
Copy link
Member

+1 on this, getting around 500 exceptions thrown on generation of a column chart in a .NET 7 application.

just to clarify, you are on .NET 7, but not using the native C# bindings? does the error persist then when using the C# bindings instead?

@kMutagene
Copy link
Member

Any news? Am I doing something wrong here?

No, i don't see anything directly wrong with your code, however i do not plan to add fixes specifically targeting .NET framework stuff, so my main goal here would be first identifying if this is happening with any modern .NET version first

@kMutagene kMutagene added Area: CSharp Status: Needs Repro This issue needs reproducible code labels Feb 13, 2023
@kMutagene
Copy link
Member

kMutagene commented Feb 13, 2023

Any news? Am I doing something wrong here?

So i tried to reproduce your error in netfx 471 anyways, and there are some things turning red for me:

trace.SetValue("x", x);
trace.SetValue("y", y);

there are no x and y objects created, your axes are called xAxis and yAxis

Layout layout = new Layout()

and the other constructor calls turn up with

image

So after fixing those to

using System;
using Microsoft.FSharp.Collections;
using Plotly.NET;
using Plotly.NET.LayoutObjects;
using static Plotly.NET.StyleParam.LinearAxisId;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            LinearAxis xAxis = new LinearAxis();
            xAxis.SetValue("title", "xAxis");
            xAxis.SetValue("zerolinecolor", "#ffff");
            xAxis.SetValue("gridcolor", "#ffff");
            xAxis.SetValue("showline", true);
            xAxis.SetValue("zerolinewidth", 2);

            LinearAxis yAxis = new LinearAxis();
            yAxis.SetValue("title", "yAxis");
            yAxis.SetValue("zerolinecolor", "#ffff");
            yAxis.SetValue("gridcolor", "#ffff");
            yAxis.SetValue("showline", true);
            yAxis.SetValue("zerolinewidth", 2);

            Layout layout = new Layout();
            layout.SetValue("xaxis", xAxis);
            layout.SetValue("yaxis", yAxis);
            layout.SetValue("title", "A Figure Specified by DynamicObj");
            layout.SetValue("plot_bgcolor", "#e5ecf6");
            layout.SetValue("showlegend", true);

            Trace trace = new Trace("line");
            trace.SetValue("x", xAxis);
            trace.SetValue("y", yAxis);


            var fig = GenericChart.Figure.create(ListModule.OfSeq(new[] { trace }), layout);
            GenericChart.fromFigure(fig).Show();
        }
    }
}

It runs without any exceptions on my machine, meaning i cannot reproduce the error with your repro instructions.

Here is the .csproj file for reference:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net471</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Plotly.NET" Version="3.0.1" />
  </ItemGroup>

</Project>

@davidpfister
Copy link
Author

Good catch. Thanks for the help. I confirm that the trace was the culprit and that everything works fine in net471.
The issue can be closed for me

@bagoette
Copy link

I am having this same issue in a .NET 7 project using Visual Studio 2022
image

I have created a repo to reproduce the error: Repo

The program executes successfully and outputs the expected chart file, but there is a stream of these Exceptions in the output window while it's running.

@dr124
Copy link

dr124 commented Sep 7, 2023

I have the same issue,
after creating a completely new project in .net 7 and adding the Plotly.NET.CSharp library as suggested
if I try to add any example then 100s of the exceptions are thrown
it eventually opens the plot after some time but this seems very wrong

@kMutagene
Copy link
Member

Sorry but i still cannot reproduce this. If i clone the provided repo, I get no such console output:

image

if I try to add any example then 100s of the exceptions are thrown

could you maybe go into detail where you see these exceptions thrown @dr124 ?

@dr124
Copy link

dr124 commented Sep 11, 2023

2023-09-11.16-57-05.mp4

Hi, I just cloned the same repo that you used and added some WriteLines

as you can see, the program takes way too much time to generate the plot - data preparation is instant but SaveSVG/Show/SaveHtml are causing some inner exceptions

I did some quick benchmarking and discovered that it happened only once when I used RunPlotlyTests() multiple times; after that it was faster and without additional exceptions.

@kMutagene
Copy link
Member

kMutagene commented Sep 12, 2023

Thanks, so this only happens in the debugger console? I'll give it a try. Weird error - the whole fun of DynamicObj is that it is - well - using dynamic.

First google results point to something like this https://stackoverflow.com/questions/31032122/runtime-binder-exception, where that error is thrown when dynamic members are accessed that are not set on an object.

However, the library only sets these values dynamically when creating objects, but never tries to access them in an unsafe way. Also, your code does not try access any of these values.

A few other notes:

as you can see, the program takes way too much time to generate the plot

That is because you are downloading chromium to render an svg from the plot.

I did some quick benchmarking and discovered that it happened only once when I used RunPlotlyTests() multiple times; after that it was faster and without additional exceptions.

Because chromium is downloaded after the first run, it should work fast after that, so this is expected.

@kMutagene
Copy link
Member

Also, i think @davidpfister might have posted the thread with the solution, i just did not understand it at the time. https://stackoverflow.com/questions/5678608/why-a-microsoft-csharp-runtimebinder-runtimebinderexception-if-the-invoked-metho seems to imply that we should expose internals from Plotly.NET to Plotly.NET.CSharp, i'll try that once i can reproduce the error.

@dr124
Copy link

dr124 commented Sep 12, 2023

Because chromium is downloaded after the first run, it should work fast after that, so this is expected.

Even if I switched to Show or SaveHtml (and removed imageexport package) there was the same delay at startup when using debugger. I think it has to do a lot with the events captured in IDE. when running without debugger it's way faster.

update: Additionally, in WPF app when this code is executed on button click, I get the exceptions everytime

@kMutagene
Copy link
Member

@dr124 sadly I can still not reproduce this. Do you have any special settings enabled for the debugger? if I execute the repo in VS the program takes a split second and no Expections are shown:

PlotlyTest.-.Microsoft.Visual.Studio.2023-09-13.20-06-50.mp4

@bagoette
Copy link

So I just ran PlotlyTest for the first time since I posted last October and I'm not seeing the error anymore.

  • The only thing I can think of that's different is that I have the .NET 8 Preview 7 SDK installed now
    • Even though this project is .NET 7, my build output appears to use the .NET 8 SDK for something
    • image

@dr124
Copy link

dr124 commented Sep 13, 2023

@dr124 sadly I can still not reproduce this. Do you have any special settings enabled for the debugger? if I execute the repo in VS the program takes a split second and no Expections are shown:
PlotlyTest.-.Microsoft.Visual.Studio.2023-09-13.20-06-50.mp4

I think it's the just my code stuff in the visual studio. I think I used it to debug/investigate some 3rd party library.
Now after enabling it - it's instant and no errors in VS. I only recalled about it when you asked about the settings.

image

@bagoette
Copy link

@dr124 sadly I can still not reproduce this. Do you have any special settings enabled for the debugger? if I execute the repo in VS the program takes a split second and no Expections are shown:
PlotlyTest.-.Microsoft.Visual.Studio.2023-09-13.20-06-50.mp4

I think it's the just my code stuff in the visual studio. I think I used it to debug/investigate some 3rd party library. Now after enabling it - it's instant and no errors in VS. I only recalled about it when you asked about the settings.

image

Yep, just tested with Enable Just My Code unchecked, and it was really slow and all of the exceptions showed up!

@kMutagene
Copy link
Member

So now we found it 🎉 - this seems to be excepted for dynamic objects. There is also an issue on Newtonsoft.Json that points to this stack overflow post.

The gist is that

Apparently that's just how dynamic works

You are setting the debugger to break when CLR exceptions are thrown (i.e. first-chance) not unhandled (i.e. second-chance). Obviously, you can untick this and it will go away, but if you want to see first-chance exceptions only from your code, then you can enable the Just My Code option. With Just My Code enabled the debugger will only break on a first-chance exception if it passes through your code. These options don't affect the behavior of your application for a user, only what the debugger does when attached.

So i think we can close this and just have to accept that this is weird default behavior 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: CSharp Status: Needs Repro This issue needs reproducible code
Projects
None yet
Development

No branches or pull requests

5 participants