The socket connection was aborted - CommunicationException

后端 未结 7 687
我寻月下人不归
我寻月下人不归 2020-12-08 10:54

Originally:

  • I thought this was a circular reference problem........turns out it\'s not.
  • The problem arose from having not configured
相关标签:
7条回答
  • 2020-12-08 11:01

    This exception occurred for me when I was returning an object with IEnumerable collections in it, and an exception occurred while one of the collection members was being retrieved. At that point, it's too late to catch it in your code, and presumably WCF is designed to disconnect the socket in that case because it's also too late to report an exception to the client, since it has already started streaming results.

    0 讨论(0)
  • 2020-12-08 11:03

    that exception is not related to Circular Reference, it's just purely timing out as you try to pump tons of data over the wire.

    The default values that comes with WCF are very very low (these have been changed in WCF 4 I believe). Have a read on these two blog posts, they should give you an idea on how to dethrottle your service:

    Creating high performance WCF services

    How to throttle a Wcf service, help prevent DoS attacks, and maintain Wcf scalability

    Update: also, there are a number of different timeouts in the WCF configuration and depending whether it's the client or server you're talking about you need to update a different timeout clause... have a read of this thread on what each one means and you should be able to figure out which one you need to bump up. Or, you could just set every timeout to int.max if you don't really care if a call can take a loong time to complete.

    0 讨论(0)
  • 2020-12-08 11:03

    In my case i was trying to instantiate a Wcf with net tcp. So, if in the bindings section of your web.config you have the "netTcpBinding" configurated like this

    <bindings>
        <netTcpBinding>
            <binding name="bindingName" closeTimeout="01:10:00" openTimeout="01:10:00" receiveTimeout="01:10:00" sendTimeout="01:10:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="10" maxReceivedMessageSize="2147483647" portSharingEnabled="true">
                <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="16384"/>
                <reliableSession ordered="true" inactivityTimeout="01:10:00" enabled="false"/>
                <security mode="None"/>
            </binding>
        </netTcpBinding>
    </bindings>
    

    Then, you need to configurate the service section defining the endpoints with their contract attribute using the namespace of your services interface and put the baseAddres as well, something like this

    <services>
      <service behaviorConfiguration="sgiBehavior" name="Enterprise.Tecnic.SGI.OP.Wcf.OP">
        <endpoint address="" behaviorConfiguration="endPointBehavior" binding="webHttpBinding" bindingConfiguration="BindingWebHttp" name="endPointHttp" contract="Enterprise.Tecnic.SGI.OP.Interface.IOP"/>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="bindingName"
          name="EndpointNetTcp" contract="Enterprise.Tecnic.SGI.OP.Interface.IOP" />
    
        <host>
          <baseAddresses>
            <!--  <add baseAddress="http://localhost:61217/OP.svc"/> -->
            <add baseAddress="http://www.Enterprise.com/Tecnic/SGI/OP/" />
            <add baseAddress="net.tcp://www.Enterprise.com/Tecnic/SGI/OP/" />           
          </baseAddresses>
        </host>
      </service>
    </services>
    

    I spent two days with this problem, but this was the only thing that worked for me.

    0 讨论(0)
  • 2020-12-08 11:06

    The WCF error:

    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 ...

    where timeouts reported are very close to 1 minute (e.g. 00:00:59.9680000) or 1 minute exactly (i.e. 00:01:00) can be caused by the message being too large and exceeding the settings for the binding.

    This can be fixed by increasing the values in the config file, e.g.:

    <binding name="MyWcfBinding" 
             maxReceivedMessageSize="10000000" 
             maxBufferSize="10000000" 
             maxBufferPoolSize="10000000" />
    

    (example values only, you may want to tune them).

    0 讨论(0)
  • 2020-12-08 11:19

    Had this problem with a long intialisation process that was being called from the OnStart event of a Windows Service Host installer. Fixed by setting the security mode and timeouts for the TCP binding.

                // Create a channel factory.
                NetTcpBinding b = new NetTcpBinding();
                b.Security.Mode = SecurityMode.Transport;
                b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
                b.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
    
                b.MaxReceivedMessageSize = 1000000;
                b.OpenTimeout = TimeSpan.FromMinutes(2);
                b.SendTimeout = TimeSpan.FromMinutes(2);
                b.ReceiveTimeout = TimeSpan.FromMinutes(10);
    
    0 讨论(0)
  • 2020-12-08 11:20

    This error can be caused by a number of things. While it was a timing issue in this case, it usually has nothing to do with timings, especially if the error is received immediately. Possible reasons are:

    • The objects used as parameters or return types in your contract don't have parameterless constructors and are not decorated with the DataContract attribute. Check the classes used as parameters or return types, but also all the types used by the public properties of those classes. If you implement a constructor with parameters for one of those classes, the compiler will not add the default parameterless constructor for you anymore, so you will need to add that yourself.
    • The default limits defined in service configuration are too low (MaxItemsInObjectGraph, MaxReceivedMessageSize, MaxBufferPoolSize, MaxBufferSize, MaxArrayLength).
    • Some public properties of your DataContract objects are read-only. Make sure all public properties have both getters and setters.
    0 讨论(0)
提交回复
热议问题