No matter what I try: The I/O operation has been aborted because of either a thread exit or an application request

1

I try to build a simple async net tcp wcf tool which will open connection, send command, receive answer (a List with 0-10 string sentences), close connection.

The problem is, I get on (self-hosted) service side always - no matter what I try - "The I/O operation has been aborted because of either a thread exit or an application request", on client side of course the corresponding errors like "Existing connection was closed by remote host" and timeouts and so on.

I tried alot for the past days but I can't get rid of it.

Client Side (running on .NET 4.0, called around once a sec):

void callservice(string mykey) {
ServiceReference1.Client c = new ServiceReference1.Client(); 
c.GetDataCompleted += c_GetDataCompleted;                             
            try {
            c.GetDataAsync(mykey);
            }
            catch (FaultException aa)
            {                    
                c.Abort();                   
            }         
       }

 private void c_GetDataCompleted(object sender, ServiceReference1.GetDataCompletedEventArgs e)
    {
        ServiceReference1.Client c = (ServiceReference1.Client)sender;
        c.GetDataCompleted -= c_GetDataCompleted;                       
        try
        {
            if (e.Result != null && e.Result.Length > 0)
            {
              ... }
            c.Close();
         }
         catch (Exception) {
           c.Abort();
         }
      }

Server Side (running on .NET4.5):

   [ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple,
InstanceContextMode=InstanceContextMode.PerCall,IncludeExceptionDetailInFaults=true)]
   public class Service1 : IMyService
    {
 public async Task<List<string>> GetData(string whatkey)
        {    
          List<string> mydatalist = new List<string>(); 
          mydatalist= await Task.Run<List<string>>(() =>
        {       
        ...
        });
    return mydatalist;
  }

What is going wrong there? Could it be that it is something not having to do with WCF at all? What could it be?

Server Side Exception:

System.Net.Sockets.SocketException, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 The I/O operation has been aborted because of either a thread exit or an application request at System.ServiceModel.Channels.SocketConnection.HandleReceiveAsyncCompleted() at System.ServiceModel.Channels.SocketConnection.OnReceiveAsync(Object sender, SocketAsyncEventArgs eventArgs) at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationAsyncFailure(SocketError socketError, Int32 bytesTransferred, SocketFlags flags) at System.Net.Sockets.SocketAsyncEventArgs.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP) System.Net.Sockets.SocketException (0x80004005): The I/O operation has been aborted because of either a thread exit or an application request 3E3

One more interesting fact: SVCLogs show me that the I/O Exeption occurs after a timespan I can define in the

<connectionPoolSettings groupName="default" leaseTimeout="00:03:00" 
idleTimeout="00:02:39" maxOutboundConnectionsPerEndpoint="20" />

settings. In this example it will occur the first time after 00:02:39. My interpretation: It closes open connections due to the settings there and that causes the Exception since the ReceiveAsync operation i.ex. was still open.

My question is so far why does client.close() not close it completely and why isn't it finished yet when it is calling the c_getdatacompleted-event? Why does the operation "hang out" for 02:39 minutes and does not come to an end?

(If I would not force the close down via the connectionpool settings I end up with hundreds of open operations if I use netstat i.ex. to display)

c#
wcf
exception
io
asked on Stack Overflow Feb 21, 2013 by Chris • edited Feb 22, 2013 by Chris

1 Answer

0

Async WCF operations (AsyncPattern=true) are implemented with the Asynchronous Programming Model. That is, you implement an operation ("Operation") with two asynchronous operations ("BeginOperation" and "EndOeration"). The client can wrap those operations with a Task (presumably with the FromAsync overload)

For example:

[ServiceContract]
public interface ISampleTaskAsync
{
    [OperationContract(AsyncPattern = true)]
    IAsyncResult BeginDoWork(int count, AsyncCallback callback, object state);

    int EndDoWork(IAsyncResult result);
}

The WCF contract does not return a Task<T>

Then, on the client you could do something like:

var proxy = new Services.SampleTaskAsyncClient();
object state = "This can be whatever you want it to be";

var task = Task<int>.Factory.FromAsync(proxy.BeginDoWork, 
    proxy.EndDoWork, 10, state);

For more information see:

If you want to use Task<T>, I believe you don't need AsyncPattern=true.

answered on Stack Overflow Feb 22, 2013 by Peter Ritchie

User contributions licensed under CC BY-SA 3.0