Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpClient PostAsJsonAsync与Newtonsoft.Json不兼容_C#_Json_Json.net_Dotnet Httpclient - Fatal编程技术网

C# HttpClient PostAsJsonAsync与Newtonsoft.Json不兼容

C# HttpClient PostAsJsonAsync与Newtonsoft.Json不兼容,c#,json,json.net,dotnet-httpclient,C#,Json,Json.net,Dotnet Httpclient,我刚在winforms应用程序中获取的内容 我的应用程序对web Api服务进行http调用,如下所示 HttpClient _client = new HttpClient(); _client.Timeout = new TimeSpan(0, 3, 0); _client.BaseAddress = new Uri("http://Myserver/MyApp"); _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQual

我刚在winforms应用程序中获取的内容

我的应用程序对web Api服务进行http调用,如下所示

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;
没什么特别的,但只要您通过nuget安装Newtonsoft.Json(V6.0.3)

突然,我在HttpResponseMessage代码行上出现了stackOverflow错误。 移除Newtonsoft,问题就解决了

问题是我使用这个库来序列化/反序列化表单中其他地方的数据

我的解决方法是使用另一个库,我只是使用System.Runtime.Serialization.Json;但这还是很奇怪,不是吗

我还应该补充一点,这是DotnetV4.0(不是4.5),我的应用程序是一个VSTO COM对象,作为附加组件在MsWord中运行


我怀疑Newtonsoft中可能存在错误,请从nuget安装“microsoft asp.net web api 2.2客户端库”,不要手动引用system.net.http.dll和system.net.http.formatting.dll。如果安装此软件包,则将同时安装正确的json.net

我在更新到新版本的Newtonsoft.json软件包后收到错误


卸载Microsoft.AspNet.WebApi.Client nugget软件包,并在升级到较新的Newtonsoft.Json软件包后重新安装,为我解决了这个问题。

如果您只需要
PostAsJsonAsync
方法,最好编写自己的扩展方法

我建议删除对
Microsoft.AspNet.WebApi.Client
(当我从这个包中使用PostAsJsonAsync时,它会抱怨找不到较旧版本的Newtonsoft.Json,但问题是我需要最新版本。我的项目目标是.net framework 4.7.2)总之

下面是您可以复制和粘贴的代码

我使用了完全限定名,因此您不必担心使用语句添加

我还使用Newtonsoft.Json库来序列化对象

public static class HttpClientExt
{
    public static async System.Threading.Tasks.Task<HttpResponseMessage> PostAsJsonAsync<T>(this HttpClient client, string requestUrl, T theObj)
    {
        var stringContent = new StringContent(
            Newtonsoft.Json.JsonConvert.SerializeObject(theObj),
            System.Text.Encoding.UTF8, "application/json");
        return await client.PostAsync(requestUrl, stringContent);
    }
}
公共静态类HttpClientExt
{
public static async System.Threading.Tasks.Task postasjssonasync(此HttpClient客户端,字符串请求URL,T theObj)
{
var stringContent=新的stringContent(
Newtonsoft.Json.JsonConvert.SerializeObject(theObj),
System.Text.Encoding.UTF8,“应用程序/json”);
return wait client.PostAsync(requestUrl,stringContent);
}
}

根据此答案修改:

如果堆栈溢出,堆栈上有什么?启用外部代码。是否启用外部代码?你的意思是我应该获取newtonsoft源代码并允许在外部类库中进行调试?我现在会找到那个版本的来源抱歉,还不清楚。我的意思是启用“显示外部代码”。这是堆栈跟踪窗口中的一个菜单选项。谢谢。我试试看。问题是它并不总是给我调试断点。这是一个VSTO ms word插件,有时ms word会发出低沉的声音completely@Crudler我猜你从来没有想到这一点?我遇到了同样的错误,我希望有一个解决方案,而不是不包括Newtonsoft.JsonI。我刚刚尝试了这个方法,但没有成功(.net framework 4.7.2)。为什么不使用
wait jsonvert.SerializeObjectAsync(theObj)