我为我的Web API应用程序设置了IIS。我的Web API中的一个函数调用SOAP Web服务。当我尝试使用我的其他项目调用我的Web API时,它会返回Unable to connect to the remote server
错误。
奇怪的是,当我在调试模式下运行我的Web API应用程序时,错误没有显示,并且它按预期工作。
以下是我如何调用Web API的草稿:
static async Task RunAsyncBlogs()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://ph-lb-d12/my_webapi/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
var success = "";
// HTTP GET
HttpResponseMessage response = await client.GetAsync("api/Controller/DoAction");
if (response.IsSuccessStatusCode)
{
success = "yes";
}
else
{
success = "no";
}
}
}
接下来,我将通过Web API解决方案调用Web服务:
假设GetXMLData
是API控制器调用的函数
public XmlDocument GetXMLData()
{
var xmlRaw = "";
XmlDocument xmldoc = new XmlDocument();
WS.Service webService = new WS.Service();
xmlRaw = webService.DoAction("TEST").OuterXml; //THIS IS WHERE THE ERROR OCCURS
xmldoc.Load(xmlRaw);
return xmldoc;
}
假设这一切都在Web API解决方案处于调试模式时有效。但是,当它不是。它返回:
<Error><Message>An error has occurred.</Message><ExceptionMessage>Unable to connect to the remote server</ExceptionMessage><ExceptionType>System.Net.WebException</ExceptionType><StackTrace> at cemws.Core.PLI.GetXMLFeed() in e:\micron\cem\PROTOTYPE\cemws\src\cemws\Core\PLI.cs:line 70 at cemws.Controllers.IntegrationController.GetPLIFeed() in e:\micron\cem\PROTOTYPE\cemws\src\cemws\Controllers\IntegrationController.cs:line 42 at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()</StackTrace><InnerException><Message>An error has occurred.</Message><ExceptionMessage>A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
10.2.1.3:8080</ExceptionMessage><ExceptionType>System.Net.Sockets.SocketException</ExceptionType><StackTrace> at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)</StackTrace></InnerException></Error>