HttpClient的PostAsJsonAsync与Newtonsoft.Json不兼容 [英] HttpClient PostAsJsonAsync incompatible with Newtonsoft.Json
问题描述
东西我刚才在我的WinForms应用程序拿起
我的应用程序的HTTP调用的Web API服务如下:
HttpClient的_client =新的HttpClient();
_client.Timeout =新时间跨度(0,3,0);
_client.BaseAddress =新的URI(HTTP:// MYSERVER / MyApp的);
_client.DefaultRequestHeaders.Accept.Add(新MediaTypeWithQualityHeaderValue(应用/ JSON));
HttpResponseMessage响应= _client.PostAsJsonAsync(API /加载项,NEWOBJECT)。结果;
没什么特别,但只要您通过的NuGet 安装Newtonsoft.Json(V6.0.3) p>
突然我得到的代码HttpResponseMessage行StackOverflow的错误。
删除Newtonsoft和问题就解决了。
问题是我是使用库序列化/反序列化在我的形式在其他地方的数据。
我的解决方法是使用一个不同的库,我只是用System.Runtime.Serialization.Json;但是这仍然是很奇怪,不是吗?
我还要补充一点,这是DOTNET V4.0(不是4.5),和我的应用程序在运行的VSTO COM对象MSWORD作为一种添加在
我猜想也许在Newtonsoft一个bug
从的NuGet安装微软asp.net的Web API 2.2客户端库,并没有提到的system.net.http.dll和手动system.net.http.formatting.dll。如果你安装这个包,那么将安装正确的json.net以及
Something I have just picked up in my winforms app
My app does an http call to a web Api service as follows
HttpClient _client = new HttpClient();
_client.Timeout = new TimeSpan(0, 3, 0);
_client.BaseAddress = new Uri("http://Myserver/MyApp");
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = _client.PostAsJsonAsync("api/Addin", newObject).Result;
Nothing fancy, but as soon as you install Newtonsoft.Json (V6.0.3) via nuget
suddenly I get a stackOverflow error on the HttpResponseMessage line of code. remove Newtonsoft, and problem is solved.
The problem is I was to use the library to serialize/deserialize data elsewhere in my form
My workaround was to use a different library, I am just using System.Runtime.Serialization.Json; but this is still really weird, no?
I should also add that this is dotnet v4.0 (not 4.5), and my app is a VSTO COM object running in MsWord as an Add On
I suspect a bug maybe in Newtonsoft
Install the "microsoft asp.net web api 2.2 client libraries" from nuget and don't refer the system.net.http.dll and system.net.http.formatting.dll manually. If you install this package then will install the correct json.net as well
这篇关于HttpClient的PostAsJsonAsync与Newtonsoft.Json不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!