无法从 ASP.Net WebAPI 2 post 数据到 SQS

Unable to post data into SQS from ASP.Net WebAPI 2

我正在尝试从 ASP.Net webapi 应用程序 post 一些数据到 AWS SQS 队列。 我正在使用 AmazonSQSClient SendMessage() 来执行此操作。

它在控制台应用程序和 windows 服务中运行良好。但是,当我从 webapi 应用程序尝试相同的操作时,SendMessage() 方法抛出以下异常:

[Amazon.Runtime.AmazonServiceException]
{"Encountered a WebException (ConnectFailure), the request cannot be retried. Either the maximum number of retries has been exceeded (4/4) or the request is using a non-seekable stream."}

内部异常:
{"An error occurred while sending the request."},{"Unable to connect to the remote server"}

堆栈跟踪:

    at Amazon.Runtime.AmazonWebServiceClient.RetryOrThrow(WebRequestState state, Exception exception) at Amazon.Runtime.AmazonWebServiceClient.
<InvokeConfiguredRequest>d__3`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.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at Amazon.Runtime.AmazonWebServiceClient.
  <InvokeConfiguredRequest>d__3`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.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at Amazon.Runtime.AmazonWebServiceClient.
    <InvokeConfiguredRequest>d__3`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.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at Amazon.Runtime.AmazonWebServiceClient.
      <InvokeConfiguredRequest>d__3`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.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at Amazon.Runtime.AmazonWebServiceClient.
        <InvokeConfiguredRequest>d__3`1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Amazon.SQS.AmazonSQSClient.SendMessage(SendMessageRequest request) at WebAPI.Alerts.Controllers.AlertsController.SendMessage(Queue
          queue, QueueRequest request) in c:\....\Controllers\AlertsController.cs:line 418

谁能帮我解决这个问题?

添加一些代码以更好地解释问题:

  private QueueResponse SendMessage(Queue queue, QueueRequest request)
    {
        var queueResponse = new QueueResponse();
        try
        {
            if (queue != null)
            {
                if (request != null)
                {
                    using (this._queueServiceClient = this.CreateQueueServiceClient(queue.AccessKeyId, queue.SecretAccessKey))
                    {
                        var request1 = new SendMessageRequest()
                        {
                            DelaySeconds = request.DelaySeconds,
                            MessageBody = request.MessageBody,
                            QueueUrl = queue.Url
                        };
                        if (request.Attributes != null && request.Attributes.Count > 0)
                            request1.MessageAttributes = Enumerable.ToDictionary<MessageAttribute, string, MessageAttributeValue>((IEnumerable<MessageAttribute>)request.Attributes, (Func<MessageAttribute, string>)(a => a.Name), (Func<MessageAttribute, MessageAttributeValue>)(b => new MessageAttributeValue()
                            {
                                DataType = b.DataType,
                                StringValue = b.Value
                            }));
                        SendMessageResponse sendMessageResponse = this._queueServiceClient.SendMessage(request1);
                        if (sendMessageResponse == null)
                            throw new Exception("No response from the Queue");
                        queueResponse.ResponseCode = ((object)sendMessageResponse.HttpStatusCode).ToString();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            queueResponse.ResponseCode = ((object)HttpStatusCode.BadRequest).ToString();
            queueResponse.ErrorMessage = ex.Message;
        }
        return queueResponse;
    }

更新 该问题似乎与代理设置有关。如果系统中开启了fiddler,则webapi成功访问队列。但如果提琴手关闭,它就会失败。

最终更新

这个问题毕竟有点傻。 Webapi 需要使用系统代理。 添加

<system.net>
    <defaultProxy enabled="true"></defaultProxy>
  </system.net>

在机器配置中解决了这个问题。

我只是将解决方案重新发布为答案,以便我可以将问题标记为已解决。

Webapi 在发布请求时需要使用系统代理。添加

<system.net>
    <defaultProxy enabled="true"></defaultProxy>
  </system.net>

在机器配置中解决了这个问题。