Ошибка веб-API С#: соединение сокета было прервано

Я использую веб-API С# для интеграции между Power Automate и AX 2012. Я получаю эту временную ошибку каждый раз, когда поток запускается в первый раз.

    {
  "className": "xxx",
  "methodName": "xxx",
  "status": "Error",
  "userName": "xxx",
  "aifURL": "xxx",
  "data": null,
  "error": {
    "ClassName": "System.ServiceModel.CommunicationException",
    "Message": "The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:09:59.9843888'.",
    "Data": {},
    "InnerException": {
      "ClassName": "System.IO.IOException",
      "Message": "The read operation failed, see inner exception.",
      "Data": {},
      "InnerException": {
        "ClassName": "System.ServiceModel.CommunicationException",
        "Message": "The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:09:59.9843888'.",
        "Data": {},
        "InnerException": {
          "NativeErrorCode": 10054,
          "ClassName": "System.Net.Sockets.SocketException",
          "Message": "An existing connection was forcibly closed by the remote host",
          "Data": {},
          "InnerException": null,
          "HelpURL": null,
          "StackTraceString": "   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)\r\n   at System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, Boolean closing)",
          "RemoteStackTraceString": null,
          "RemoteStackIndex": 0,
          "ExceptionMethod": "8\nReceive\nSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\nSystem.Net.Sockets.Socket\nInt32 Receive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags)",
          "HResult": -2147467259,
          "Source": "System",
          "WatsonBuckets": null
        },
        "HelpURL": null,
        "StackTraceString": "   at System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, Boolean closing)\r\n   at System.ServiceModel.Channels.SocketConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)\r\n   at System.ServiceModel.Channels.ConnectionStream.Read(Byte[] buffer, Int32 offset, Int32 count)\r\n   at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)\r\n   at System.Net.Security.NegotiateStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)\r\n   at System.Net.Security.NegotiateStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)\r\n   at System.Net.Security.NegotiateStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)",
        "RemoteStackTraceString": null,
        "RemoteStackIndex": 0,
        "ExceptionMethod": "8\nReadCore\nSystem.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\nSystem.ServiceModel.Channels.SocketConnection\nInt32 ReadCore(Byte[], Int32, Int32, System.TimeSpan, Boolean)",
        "HResult": -2146233087,
        "Source": "System.ServiceModel",
        "WatsonBuckets": null
      },
      "HelpURL": null,
      "StackTraceString": "   at System.Net.Security.NegotiateStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)\r\n   at System.Net.Security.NegotiateStream.Read(Byte[] buffer, Int32 offset, Int32 count)\r\n   at System.ServiceModel.Channels.StreamConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)",
      "RemoteStackTraceString": null,
      "RemoteStackIndex": 0,
      "ExceptionMethod": "8\nProcessRead\nSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\nSystem.Net.Security.NegotiateStream\nInt32 ProcessRead(Byte[], Int32, Int32, System.Net.AsyncProtocolRequest)",
      "HResult": -2146232800,
      "Source": "System",
      "WatsonBuckets": null
    },
    "HelpURL": null,
    "StackTraceString": "   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)\r\n   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)\r\n   at SXA_FlowIntegrationWebAPI.AX2012.SXAFlowIntegrationService.callMethod(SXAFlowIntegrationServiceCallMethodRequest request)\r\n   at SXA_FlowIntegrationWebAPI.AX2012.SXAFlowIntegrationServiceClient.callMethod(CallContext CallContext, String _className, String _methodName, String _data)\r\n   at SXA_FlowIntegrationWebAPI.Controllers.MainController.CallMethod(ClassMethodCallRequest request)",
    "RemoteStackTraceString": "\r\nServer stack trace: \r\n   at System.ServiceModel.Channels.StreamConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)\r\n   at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.SendPreamble(IConnection connection, ArraySegment`1 preamble, TimeoutHelper& timeoutHelper)\r\n   at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.DuplexConnectionPoolHelper.AcceptPooledConnection(IConnection connection, TimeoutHelper& timeoutHelper)\r\n   at System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnection(TimeSpan timeout)\r\n   at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(TimeSpan timeout)\r\n   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)\r\n   at System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout)\r\n   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)\r\n   at System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan timeout, CallOnceManager cascade)\r\n   at System.ServiceModel.Channels.ServiceChannel.EnsureOpened(TimeSpan timeout)\r\n   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)\r\n   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)\r\n   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)\r\n\r\nException rethrown at [0]: \r\n",
    "RemoteStackIndex": 1,
    "ExceptionMethod": "8\nHandleReturnMessage\nmscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\nSystem.Runtime.Remoting.Proxies.RealProxy\nVoid HandleReturnMessage(System.Runtime.Remoting.Messaging.IMessage, System.Runtime.Remoting.Messaging.IMessage)",
    "HResult": -2146233087,
    "Source": "mscorlib",
    "WatsonBuckets": null
  }
}

Ниже приведен код C# для контроллера.

public class MainController : ApiController
    {
        [ActionName("Method")]
        [HttpPost]
        [Authorize]
        public IHttpActionResult CallMethod(ClassMethodCallRequest request)
        {
            dynamic retValue;
            bool success = false;
            AX2012.SXAFlowIntegrationServiceClient client = null;        

            try
            {
                client = new AX2012.SXAFlowIntegrationServiceClient();

                var callContext = new AX2012.CallContext();
                callContext.Company = request.companyId;
                callContext.LogonAsUser = User.Identity.Name;

                client.InnerChannel.OperationTimeout = new TimeSpan(0, 10, 00);
                client.Endpoint.Address = new System.ServiceModel.EndpointAddress(request.aifURL);

                retValue = client.callMethod(callContext, request.className, request.methodName, request.dataJsonStr);
                client.Close();
                success = true;

            }
            catch (Exception e)
            {
                return Json(new ClassMethodCallResponse
                {
                    status = "Error",
                    userName = User.Identity.Name,
                    className = request.className,
                    methodName = request.methodName,
                    aifURL = request.aifURL,
                    error = e
                });
            }
            finally
            {
                if (!success)
                {
                    client.Abort();
                }
            }
            
            return Json(new ClassMethodCallResponse
            {
                status = "Success",
                userName = User.Identity.Name,
                className = request.className,
                methodName = request.methodName,
                aifURL = request.aifURL,
                data = retValue,
            });
        }

    }

Эта проблема заставляет повторно отправить долгий процесс. Ошибка не появляется во второй раз, но снова, если я запускаю поток через некоторое время, ошибка возникает снова.

Ошибка связана с тайм-аутом? Я не понимаю основную причину этой ошибки.


person Dhruvil Sheth    schedule 23.12.2020    source источник
comment
Из части сообщения об исключении Local socket timeout was '00:09:59.9843888' и вашего OperationTimeout = new TimeSpan(0, 10, 0) я предполагаю, что операция занимает более 10 минут. После этого второй вызов работает, потому что вызываемый объект может иметь некоторые данные, уже загруженные в память/кэшированные где-то.   -  person G Wimpassinger    schedule 23.12.2020
comment
Нет.. На самом деле операция занимает всего несколько секунд. Один процесс занимает больше времени, для которого я увеличил время ожидания. Но обычно 1-й процесс длится всего несколько секунд.   -  person Dhruvil Sheth    schedule 23.12.2020
comment
@DhruvilSheth То, что вы говорите, не соответствует сообщению об ошибке? Так что либо вы, либо сообщение неверно, не так ли? Сделайте несколько операторов отладки в своем коде, чтобы вывести, на каком этапе процесса он находится, и/или проверьте таблицу исключений AX через SystemAdministration\Periodic\Services and Application Integration Framework\Exceptions. Это похоже либо на связь, либо на тайм-аут. Возможно, есть шаг взаимодействия с пользователем (например, всплывающее окно «Вы уверены?»), который вы не видите, и который может блокировать весь процесс.   -  person Alex Kwitny    schedule 28.12.2020
comment
@AlexKwitny Мы видим то же самое сообщение об ошибке, в котором упоминается тайм-аут в 5 минут (совпадающий с SendTimeout нашей службы), но ошибка появляется только через 1 или 2 секунды после вызова. Так что сообщение об ошибке не очень полезно.   -  person Evgeniy Berezovsky    schedule 09.03.2021
comment
@DhruvilSheth Не могли бы вы решить это? Сегодня мы несколько раз получали одну и ту же ошибку для службы net.tcp, и я предполагаю, что это зависит от отправленных данных, несмотря на сообщение об ошибке. Взгляните на этот ответ stackoverflow.com/a/17983639 для получения дополнительных идей - к сожалению, в нашем случае это не помогло.   -  person Evgeniy Berezovsky    schedule 09.03.2021
comment
@GWimpassinger Я бы сказал, что операция была прервана после OperationTimeout - 00:09:59.9843888, другими словами, около 15 миллисекунд. Я могу воспроизвести это, установив MaxBufferSize на очень низкое значение. Вышеупомянутая ошибка появится немедленно и со значением на несколько миллисекунд меньше, чем (в моем случае) SendTimeout.   -  person Evgeniy Berezovsky    schedule 09.03.2021
comment
@EvgeniyBerezovsky 15 миллисекунд? Тайм-аут client.InnerChannel.OperationTimeout = new TimeSpan(0, 10, 00);, я думаю, 10 минут.   -  person Alex Kwitny    schedule 09.03.2021
comment
@AlexKwitny Посмотрите на отметку времени в сообщении об ошибке OP: 00:09:59.9843888. Это плюс 15 миллисекунд равняется 10-минутному таймауту OperationTimeout.   -  person Evgeniy Berezovsky    schedule 10.03.2021
comment
Я решил это, используя цикл try in while, установив счетчик повторов равным 1. Обычно я получаю эту ошибку каждый раз в первый раз. Возможно, когда процесс запускается в первый раз через 10-15-20 минут (точно не знаю время), то эта проблема возникла. Поэтому я использовал повторную попытку, чтобы повторить попытку, если система выдает ошибку сокета.   -  person Dhruvil Sheth    schedule 11.03.2021