安装Reactive Extensions后将Newtonsoft Json与Dynamic一起使用时发生RuntimeBinderException [英] RuntimeBinderException while using Newtonsoft Json with dynamic after installing Reactive Extensions

查看:119
本文介绍了安装Reactive Extensions后将Newtonsoft Json与Dynamic一起使用时发生RuntimeBinderException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码:

public async Task<string> LoginAsync(string username, string password)
{
    //removed irrelevant code from snippet

    var response = await JSonConvert.DeSerializeObjectAsync<dynamic>(responseText);
    return (response.success == SUCCESS ? response.data.sid : String.Empty);
}

在我通过NuGet安装了Reactive Extensions之前,此方法一直有效.
从这一点开始,只需在方法的最后一行抛出RuntimeBinderException:

This worked find until I installed Reactive Extensions through NuGet.
From that point on the last line of the method simply throw a RuntimeBinderException:

{'Newtonsoft.Json.Linq.JObject'不包含针对 '成功'}

{"'Newtonsoft.Json.Linq.JObject' does not contain a definition for 'success'"}

现在使其工作的唯一方法是使用以下方法:

The only way to get it working now is to use this:

return (response["success"].ToString() == SUCCESS ? response["data"]["sid"].ToString() : String.Empty);

我尝试了另一个JSon库(JsonFX2.0),但同时也遇到了RuntimeBinderException:

I tried another JSon library (JsonFX2.0) but I'm getting a RuntimeBinderException as well:

{'ExpandoObject'不包含 '成功'}

{"'ExpandoObject' does not contain a definition for 'success'"}

服务返回的json看起来像这样:

The json returned by the service looks like this:

{
  "data": {
    "sid": "sK7gyGm1kfEUc"
  },
  "success": true
}

我尝试删除Reactive Extensions引用,但这无济于事.

I've tried removing the Reactive Extensions references, but that doesn't help.

那么这里发生了什么?

So what happened here?

更新

我在VS2013中创建了一个新项目,并复制了原始解决方案中的源代码. 这只是解决方案中的所有代码,因为它只是一个游乐场解决方案.

I created a new project in VS2013 and copied the source code from the original solution. That's all the code in the solution as it was just a playground solution.

然后,我使用NuGet添加了JSON.NET库(如前所述),运行了解决方案,它可以按预期工作.然后,我像以前一样通过NuGet添加了反应式扩展库,该解决方案仍然按预期运行.我没有在任何时候更改源代码(因为我在原始解决方案中尚未在代码中使用反应式扩展)

I then added the JSON.NET library using NuGet (as before), ran the solution and it simply works as expected. I then added the reactive extensions libraries through NuGet as before and the solution still worked as expected. I did not change the source code at any point (as I did not use the reactive extensions in code yet in the original solution)

所以我不知道是什么原因导致了原始解决方案(仍然无效)

So I have no idea what caused it in the original solution (which still doesn't work)

更新2

我多次运行该代码的工作版本,有时 无需更改任何内容,相同的RuntimeBinderException再次开始弹出.

I was running the working version of the code multiple times and at some point without changing anything the same RuntimeBinderException started popping up again.

我已经检查了服务器的响应,它与以前完全一样. 我正在谈论的无关的代码:

I've checked the response from the server, it's exactly the same as before. The irrelevant code I was talking about:

public async Task<string> LoginAsync(string username, string password)
{
    var request = new Request(hostname, ApiName.SYNO_API_AUTH, 2, ApiPath.AUTHCGI, ApiMethod.LOGIN);
    request.AddParameter("account", username);
    request.AddParameter("passwd", password);
    request.AddParameter("format", "sid");

    var responseText = await client.DownloadStringTaskAsync(request.ToString());    
    var response = await JsonConvert.DeserializeObjectAsync<dynamic>(responseText);
    return response.success == TRUE ? response.data.sid : String.Empty;
}

这是第一个被调用的方法,现在,最后一行再次抛出RuntimeBinder异常.我非常有信心,如果我将所有内容复制到一个新的解决方案中,它将再次起作用. (我会做的)

This is the first method that is called and now once again, the last line throws the RuntimeBinder exception. And I'm pretty confident that if I were to copy everything to a new solution it'll just work again. (which I will do)

更新3

所以现在我有了停止工作的解决方案.我带走了Program.cs的全部内容(因为这只是一个用于测试的控制台应用程序),并将其复制到了新的解决方案中. 通过nuget添加了程序包引用并执行了该应用程序,它很简单.

So now I've got the solution that stopped working. I took the entire content of Program.cs (since this is just a console app for testing) and copied it to a new solution. Added the package references through nuget and executed the app, it simply works.

因此看来,由于某种原因,Visual Studio正在执行某些操作以使其无法正常工作.

So it seems that for some reason visual studio is doing something that stops it from working.

更新4

对于无效的解决方案.如果我在Visual Studio中调试解决方案 但是,如果我打开bin/Debug目录并执行,则会得到RuntimeBinderException. 直接运行该程序就可以了.如果我从Visual Studio中运行解决方案 无需调试它就可以正常运行.因此,在启用调试器时遇到了一个问题.我还发现了另一个类似问题的问题:使用Newtonsoft解析json时出现RuntimeBinderException杰森

For the solution that does not work. If I debug the solution from within Visual Studio I get the RuntimeBinderException if however, I open the bin/Debug directory and execute the program directly it runs just fine. If I run the solution from within Visual Studio without debugging it runs just fine. So I'm running into an issue when the debugger is enabled. I've also found another question with a similar problem: RuntimeBinderException when parsing json with Newtonsoft.Json

推荐答案

我在VS 2015中遇到了这个确切问题.

I hit this exact issue in VS 2015.

解决方法是在工具">选项">调试">常规"中启用仅我的代码".

The fix was to enable "Just My Code" in Tools > Options > Debugging > General.

这篇关于安装Reactive Extensions后将Newtonsoft Json与Dynamic一起使用时发生RuntimeBinderException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆