none
Transporting a moderately sized message of 5 Mio objects throws CommunicationException RRS feed

  • Question

  • Hello everybody,

    I'm experimenting with WCF and want to transfer a pretty big chunk of data using NetTcpBinding. Unfortunately I'm getting a CommunicationException "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 '23:59:58.8999900'" after I tried transferring a collection with 5 millions records (less works without any problems). The exception is being thrown after 10-11 seconds.
    I've already tried increasing all possible timeouts (Open/Close/Send/Receive on both client and server sides) and limits (MaxReceivedMessageSize, MaxBufferPoolSize, MaxBufferSize, MaxItemsInObjectGraph) and would really appreciate your help.

    Here is the server code

    class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("net.tcp://localhost:8089/hello");
    
            using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
            {
                NetTcpBinding binding = new NetTcpBinding();
                binding.MaxReceivedMessageSize = int.MaxValue;
                binding.MaxBufferPoolSize = int.MaxValue;
                binding.MaxBufferSize = int.MaxValue;
    
                binding.OpenTimeout = new TimeSpan(23, 59, 59);
                binding.CloseTimeout = new TimeSpan(23, 59, 59);
                binding.ReceiveTimeout = new TimeSpan(23, 59, 59);
                binding.SendTimeout = new TimeSpan(23, 59, 59);
    
                host.AddServiceEndpoint(typeof(IHelloWorldService), binding, baseAddress);
                
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = false;
                host.Description.Behaviors.Add(smb);
    
                ServiceDebugBehavior behavior = host.Description.Behaviors.Find<ServiceDebugBehavior>();
    
                if (behavior != null)
                {
                    behavior.IncludeExceptionDetailInFaults = true;
                }
                else
                {
                    host.Description.Behaviors.Add(
                        new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
                }
    
                host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
    
                host.Open();
    
                Console.WriteLine("The service is ready at {0}", baseAddress);
                Console.WriteLine("Press <Enter> to stop the service.");
                Console.ReadLine();
    
                // Close the ServiceHost.
                host.Close();
            }
    
        }
    }
    
    [ServiceContract]
    public interface IHelloWorldService
    {
        [OperationContract]
        ICollection<PortfolioDTO> SayHello();
    }
    
    public class HelloWorldService : IHelloWorldService
    {
        public ICollection<PortfolioDTO> SayHello()
        {
            List<PortfolioDTO> result = new List<PortfolioDTO>();
            for (int i = 0; i < 5000000; i++)
            {
                result.Add(new PortfolioDTO{Id=i, Name=i.ToString(), SomeValue=i*2, Payload="muhahahahahahahahahahahahaha"});
            }
    
            return result;
        }
    }
    
    public class PortfolioDTO
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public double SomeValue { get; set; }
        public string Payload { get; set; }
    }

    And the client code 

    static void Main(string[] args)
    {
    	ChannelFactory<IHelloWorldService> channelFactory = null;
    	IHelloWorldService client = null;
    	
    	PortfolioDTO[] data = null;
    	
    	NetTcpBinding binding = new NetTcpBinding();
    	binding.MaxReceivedMessageSize = int.MaxValue;
    	binding.MaxBufferPoolSize = int.MaxValue;
    	binding.MaxBufferSize = int.MaxValue;
    	
    	binding.OpenTimeout = new TimeSpan(23, 59, 59);
    	binding.CloseTimeout = new TimeSpan(23, 59, 59);
    	binding.ReceiveTimeout = new TimeSpan(23, 59, 59);
    	binding.SendTimeout = new TimeSpan(23, 59, 59);
    	
    	EndpointAddress endpointAddress = new EndpointAddress("net.tcp://localhost:8089/hello");
    	channelFactory = new ChannelFactory<IHelloWorldService>(binding, endpointAddress);
    	
    	foreach (OperationDescription op in channelFactory.Endpoint.Contract.Operations)
    	{
    		var dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>();
    		if (dataContractBehavior != null)
    		{
    			dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
    		}
    	}
    	
    	client = channelFactory.CreateChannel();
    	((IClientChannel)client).OperationTimeout = new TimeSpan(23, 59, 59);
    	
    	data = client.SayHello();
    	
    	IClientChannel channel = (IClientChannel)client;
    	channel.Close();	
    	channelFactory.Close();
    }

    BTW, would also appreciate a hint how I can debug such issues. Setting ServiceDebugBehavior.IncludeExceptionDetailInFaults doesn't help much.
    Monday, January 6, 2014 2:46 PM

Answers

  • Hi,

    >>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 '23:59:58.8999900'

    When we use the reliablesession(such as wshttpbinding and nettcpbinding), we should also try increase the inActivityTimeout value. Here is a blog entry which has mentioned the session related timeout setting:
    #Session Lifetime on the Server
    http://blogs.msdn.com/drnick/archive/2007/06/26/session-lifetime-on-the-server.aspx
    Also please try to use try-finally where you can call close on the client object and if the communication channel faulted you can call abort on the client object.

    >>how I can debug such issues

    For debug this issue, please try to enable the wcf tracing to find the root cause:

    The following configuration taken from MSDN can be applied to enable tracing on your WCF service.

    <configuration>
      <system.diagnostics>
        <sources>
          <source name="System.ServiceModel"
                  switchValue="Information, ActivityTracing"
                  propagateActivity="true" >
            <listeners>
                 <add name="xml"/>
            </listeners>
          </source>
          <source name="System.ServiceModel.MessageLogging">
            <listeners>
                <add name="xml"/>
            </listeners>
          </source>
          <source name="myUserTraceSource"
                  switchValue="Information, ActivityTracing">
            <listeners>
                <add name="xml"/>
            </listeners>
          </source>
        </sources>
        <sharedListeners>
            <add name="xml"
                 type="System.Diagnostics.XmlWriterTraceListener"
                 initializeData="Error.svclog" />
        </sharedListeners>
      </system.diagnostics>
    </configuration>

    And fiddler is also a good tool for debuging this:
    #Fiddler:
    http://fiddler2.com/ .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Andrej Kuklin Friday, January 10, 2014 12:01 PM
    Tuesday, January 7, 2014 4:49 AM
    Moderator
  • Thank you, Amy, the WCF tracing helped. It was an InsufficientMemoryException thrown when serializing the DataContract on the service side. I've recompiled the server and the client for x64 platform, and the problem was gone.
    • Marked as answer by Andrej Kuklin Friday, January 10, 2014 12:01 PM
    • Edited by Andrej Kuklin Friday, January 10, 2014 12:02 PM typo
    Friday, January 10, 2014 12:01 PM

All replies

  • Hi,

    >>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 '23:59:58.8999900'

    When we use the reliablesession(such as wshttpbinding and nettcpbinding), we should also try increase the inActivityTimeout value. Here is a blog entry which has mentioned the session related timeout setting:
    #Session Lifetime on the Server
    http://blogs.msdn.com/drnick/archive/2007/06/26/session-lifetime-on-the-server.aspx
    Also please try to use try-finally where you can call close on the client object and if the communication channel faulted you can call abort on the client object.

    >>how I can debug such issues

    For debug this issue, please try to enable the wcf tracing to find the root cause:

    The following configuration taken from MSDN can be applied to enable tracing on your WCF service.

    <configuration>
      <system.diagnostics>
        <sources>
          <source name="System.ServiceModel"
                  switchValue="Information, ActivityTracing"
                  propagateActivity="true" >
            <listeners>
                 <add name="xml"/>
            </listeners>
          </source>
          <source name="System.ServiceModel.MessageLogging">
            <listeners>
                <add name="xml"/>
            </listeners>
          </source>
          <source name="myUserTraceSource"
                  switchValue="Information, ActivityTracing">
            <listeners>
                <add name="xml"/>
            </listeners>
          </source>
        </sources>
        <sharedListeners>
            <add name="xml"
                 type="System.Diagnostics.XmlWriterTraceListener"
                 initializeData="Error.svclog" />
        </sharedListeners>
      </system.diagnostics>
    </configuration>

    And fiddler is also a good tool for debuging this:
    #Fiddler:
    http://fiddler2.com/ .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Andrej Kuklin Friday, January 10, 2014 12:01 PM
    Tuesday, January 7, 2014 4:49 AM
    Moderator
  • Thank you, Amy, the WCF tracing helped. It was an InsufficientMemoryException thrown when serializing the DataContract on the service side. I've recompiled the server and the client for x64 platform, and the problem was gone.
    • Marked as answer by Andrej Kuklin Friday, January 10, 2014 12:01 PM
    • Edited by Andrej Kuklin Friday, January 10, 2014 12:02 PM typo
    Friday, January 10, 2014 12:01 PM