gpt4 book ai didi

c#-4.0 - Windows azure - 无法从传输连接读取数据: An existing connection was forcibly closed by the remote host

转载 作者:行者123 更新时间:2023-12-02 23:51:07 26 4
gpt4 key购买 nike

使用 JsonTextReader 读取数据时出现以下异常

无法从传输连接读取数据:现有连接被远程主机强制关闭。

JsonTextReader jsonReader - 参数

 while (hasRecords(jsonReader, JsonToken.StartObject, null, null)) //Row
{
...

//it's ok to read this all into memory - it's just one row's worth of data
JArray values = (JArray)JToken.ReadFrom(jsonReader);

此外,为了更清楚起见,还包括 HttpPost 实现的代码

 HttpClientHandler handler = new HttpClientHandler() { Credentials = taskProfileInfo.Credential };

HttpClient httpClient = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(taskProfileInfo.CommandTimeout) };
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


HttpResponseMessage response;

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
request.Content = new StringContent(postBody, Encoding.UTF8, "application/json");

response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);

response.EnsureSuccessStatusCode();

//using (var responseStream = await response.Content.ReadAsStreamAsync())
//{
// using (var reader = new StreamReader(responseStream))
// {
// responseFromAPI = reader.ReadToEnd();
// }
//}

return new JsonTextReader(new StreamReader(await response.Content.ReadAsStreamAsync()));

感谢任何人可以帮助我们..

编辑:请注意,我们能够在本地调试它并且工作正常。只有当我们在 Azure 云服务中将其作为辅助角色运行时才会出现问题。

最佳答案

我终于解决了这个问题。只是为了关闭这个(可能对某人有帮助) -

After doing remote debugging我们发现以下内部异常:{“请求已中止:请求已取消。”}

而且,此问题的根本原因是我们将超时设置为小于实际读取 (JsonTextReader) 操作所需的超时时间。下面的代码行设置超时:

 HttpClient httpClient = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(taskProfileInfo.CommandTimeout) };

因此,修复方法是增加超时值,以便读取数据时请求不会被取消。

关于c#-4.0 - Windows azure - 无法从传输连接读取数据: An existing connection was forcibly closed by the remote host,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26018440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com