SendAsync无法从外部服务器(Web API2)工作

时间:2016-04-21 15:04:36

标签: c# asynchronous asp.net-web-api asp.net-web-api2

我们遇到了来自服务器的sendAsync post post无法正常工作的情况。这是我的方案

我们在内部网络(DMZ)之外的服务器上托管了一个Web API,它实现了一个简单的GET

Public HttpResponseMessage Get(strind id){ (API 1)

//do some work
using(HttpClient client = new HttpClient()){     

//We Invoke another web API which is hosted inside our network and do a post   //    as

HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, url);
message.Content = new StringContent(someStr);
message.Content.Header.ContentType = new MediaTypeHeaderValue("application/json");

try{
var response = client.SendAsync(message).Result (API 2)

}
catch(Exception e){
//Something
 }
}

发布SendAsync()。结果失败,出现以下异常

  

在System.Threading.Tasks.Task`1.GetResultCore(布尔值waitCompletionNotification)中,消息显示为发生一个或多个错误。

内心感受也没有说好话。它只是InnerException.StackTrace变为空白而InnerException.Message是发送请求时发生错误。

为了测试通信,我们从外部服务器为API 2运行了一个GET请求,它工作正常,但是POST不起作用。

当我们的内部服务器上托管API(用于获取和执行帖子的另一个)时,一切正常。

任何建议为什么从外部服务器执行时可能无法正常工作?

1 个答案:

答案 0 :(得分:2)

简短的回答:无论我打赌什么,你都要检查那些内部异常并找出确切的信息。

更长的回答: 事实上,当您在内部网络中托管应用程序时,代码正常工作(POST调用成功),当移动到DMZ时,它失败强烈表示服务器使调用无法访问远程端点。

我敢打赌你的App Server和API 2之间缺乏网络连接。