微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C# Web API 错误:套接字连接已中止

如何解决C# Web API 错误:套接字连接已中止

我正在使用 C# Web API 在 power automatic 和 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.","InnerException": {
        "ClassName": "System.ServiceModel.CommunicationException","InnerException": {
          "NativeErrorCode": 10054,"ClassName": "System.Net.sockets.socketException","Message": "An existing connection was forcibly closed by the remote host","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,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,System.Net.sockets.socketFlags)","HResult": -2147467259,"Source": "System","WatsonBuckets": null
        },"StackTraceString": "   at System.ServiceModel.Channels.socketConnection.ReadCore(Byte[] buffer,Boolean closing)\r\n   at System.ServiceModel.Channels.socketConnection.Read(Byte[] buffer,TimeSpan timeout)\r\n   at System.ServiceModel.Channels.ConnectionStream.Read(Byte[] buffer,Int32 count)\r\n   at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer,Int32 count)\r\n   at System.Net.Security.NegotiateStream.StartFrameHeader(Byte[] buffer,Int32 count,AsyncProtocolRequest asyncRequest)\r\n   at System.Net.Security.NegotiateStream.StartReading(Byte[] buffer,AsyncProtocolRequest asyncRequest)\r\n   at System.Net.Security.NegotiateStream.ProcessRead(Byte[] buffer,AsyncProtocolRequest asyncRequest)","ExceptionMethod": "8\nReadCore\nSystem.ServiceModel,PublicKeyToken=b77a5c561934e089\nSystem.ServiceModel.Channels.socketConnection\nInt32 ReadCore(Byte[],System.TimeSpan,Boolean)","HResult": -2146233087,"Source": "System.ServiceModel","WatsonBuckets": null
      },"StackTraceString": "   at System.Net.Security.NegotiateStream.ProcessRead(Byte[] buffer,AsyncProtocolRequest asyncRequest)\r\n   at System.Net.Security.NegotiateStream.Read(Byte[] buffer,Int32 count)\r\n   at System.ServiceModel.Channels.StreamConnection.Read(Byte[] buffer,TimeSpan timeout)","ExceptionMethod": "8\nProcessRead\nSystem,PublicKeyToken=b77a5c561934e089\nSystem.Net.Security.NegotiateStream\nInt32 ProcessRead(Byte[],System.Net.AsyncProtocolRequest)","HResult": -2146232800,"WatsonBuckets": 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,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,PublicKeyToken=b77a5c561934e089\nSystem.Runtime.Remoting.Proxies.RealProxy\nVoid HandleReturnMessage(System.Runtime.Remoting.Messaging.IMessage,System.Runtime.Remoting.Messaging.IMessage)","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",data = retValue,});
        }

    }

这个问题迫使重新提交一个漫长的过程。第二次运行后不会弹出错误,但如果我在一段时间后再次运行流程,则会再次抛出错误

错误是否与超时有关?我没有找到此错误的根本原因。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?