运行在共享网络上执行大量Web服务调用的程序是否注定要失败?

时间:2019-02-08 16:43:26

标签: c# .net dotnet-httpclient

这是针对以下问题的后续问题:error creating the web proxy specified in the 'system.net/defaultproxy' configuration section

我有一个已部署到远程服务器的C#控制台应用程序。该程序运行良好,直到尝试调用外部Web服务为止,此时它以我在下面描述的方式崩溃。

根据那里被接受的答案,我的应用程序的最初问题是由我从共享网络驱动器启动它引起的。 (我得到的原始异常与链接问题中描述的异常非常相似)。果然,就像答案中所说的那样,当我从服务器上的本地驱动器启动它时,它就可以正常工作。

我现在正在尝试确定是否有任何方法可以从共享的网络驱动器启动我的程序,或者整个方法注定要失败,我应该放弃这个尝试其他尝试。

作为进一步的背景知识,我的程序“驻留”在远程服务器上,它将由另一个“驻留”在不同服务器上的自动化过程启动。我试图确定确切的位置,以便其他服务器上的进程可以访问它。

我考虑过尝试仅使用Windows Task Scheduler或类似工具,但我认为这不是一个好的解决方案,因为存在大量逻辑,确切说明应使用什么命令行参数以及程序生成后如何分发文件。另外,远程进程提供了各种服务,例如自动错误通知,这些服务是无法从相关服务器获得的。

我尝试了this answer中列出的修复程序,并将以下行添加到了我的App.config:

<system.net>
    <defaultProxy enabled="false" useDefaultCredentials="false">
    </defaultProxy>
  </system.net>

修复了原始异常。但是,当我从共享驱动器运行时,当我尝试使用HttpClient进行Web服务调用时,会出现以下异常:

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: Unable to connect to the remote server. ---> System.Net.Sockets.SocketException: An invalid argument was supplied
   at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
   at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
   at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
   at System.Net.Connection.CompleteStartConnection(Boolean async, HttpWebRequest httpWebRequest)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Reports.Models.User.<GetUsersWorkflow>d__65`1.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 [Class].<GetCSV>d__1.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.ValidateEnd(Task task)
   at [Namespace].MainWindow.<<runReport_Click>b__9_0>d.MoveNext()

但是,当我尝试从个人计算机上的本地驱动器(而不是服务器上的 )运行此驱动器时,会出现完全不同的错误:

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The remote name could not be resolved: '[API location]'
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Reports.Models.User.<GetUsersWorkflow>d__65`1.MoveNext() in [Location where:line 492
--- 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.GetResult()
   at [File Name].<GetCSV>d__1.MoveNext() in [File Name]:line 98
--- 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 [Namespace].MainWindow.<<runReport_Click>b__9_0>d.MoveNext() in [location]\MainWindow.xaml.cs:line 154

相关行492是下面的GetAsync(url)调用:

HttpClient client = HttpClientConstructor.GetHttpClient(false);
HttpResponseMessage message = await client.GetAsync(url);

我开始怀疑这是错误的方法,除了从该位置启动程序外,我还应该尝试其他方法。是正确的,还是我错过了什么?有什么方法可以使这项工作奏效,还是我需要找到其他解决方案?

0 个答案:

没有答案