Failed to send data to SQS from ASP.Net WebAPI 2

I am trying to send some data to an AWS SQS queue from an ASPAP WebAPI application. I am using AmazonSQSClient SendMessage () for this.

It works great in console apps and windows services. However, when I try to do the same from a webapi application, the SendMessage () method throws the following exception:

[Amazon.Runtime.AmazonServiceException]
{"A WebException (ConnectFailure) was encountered, the request could not be retry. Either the maximum retry times were exceeded (4/4), or the request is using a non-searchable stream."}

Internal Exception :
{"An error occurred while sending the request.}, {" Failed to connect to the remote server "}

Stack trace:

    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

      

Can anyone help me with this?

Adding code to explain the problem in more detail:

  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;
    }

      

UPDATE The problem seems to be related to the proxy settings. If a script is open on the system, then webapi successfully accesses the queue. But it doesn't work if the violinist is closed.

Final update

In the end, the problem was a little silly. Webapi needed to use a system proxy. Adding

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

      

the problem is resolved in the machine configuration.

+3


source to share


1 answer


I'm just redirecting the solution as an answer so that I can mark the issue as resolved.

Webapi must use a system proxy when sending requests. Adding



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

      

the problem is resolved in the machine configuration.

+1


source







All Articles