我正在通过.net客户端桌面应用程序正在使用的dll文件调用网络api。 Dll文件写在.Net 4.0
中。
有时我在这条线上经常出错。
var postResponse = client.PostAsJsonAsync(path,Input).Result;
出现错误消息后,我检查了IIS和Web服务日志文件。根据日志文件,上面的代码行没有进入Web服务,因为Web方法将所有活动记录在其自己的日志文件中。
有没有办法避免超时错误。
DLL文件代码
private T CallService<T>(string path)
{
using (var client = new HttpClient())
{
client.BaseAddress = EndpointAddress;
client.DefaultRequestHeaders.accept.Clear();
client.DefaultRequestHeaders.accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.Timeout = TimeSpan.FromSeconds(30);
var response = client.PostAsJsonAsync(path,Input).Result;
if (response.IsSuccessful)
{
var serviceResponse = response.Content.ReadAsAsync<T>().Result;
// return service response;
}
else
{
// return default (T)
}
}
}
网络服务方法
[HttpPost]
public CusResponse getcustomer(CustomerOperationInput input)
{
}
这是超时时的标头数据
{StatusCode: 500,ReasonPhrase: 'Internal Server Error',Version: 1.1,Content: System.Net.Http.StreamContent,Headers:
{
Pragma: no-cache
Connection: close
Cache-Control: no-cache
Date: Wed,06 Nov 2019 11:06:45 GMT
Server: microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 36
Content-Type: application/json; charset=utf-8
Expires: -1
}}
IIS跟踪文件中的错误
<EventData>
<Data Name="ContextId">{00000000-0000-0000-230F-008000000019}</Data>
<Data Name="ModuleName">ManagedPipelineHandler</Data>
<Data Name="Notification">128</Data>
<Data Name="HttpStatus">500</Data>
<Data Name="HttpReason">Internal Server Error</Data>
<Data Name="HttpSubStatus">0</Data>
<Data Name="ErrorCode">0</Data>
<Data Name="ConfigExceptionInfo"></Data>
</EventData>