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)

4

1 回答 1

0

异步 WCF 操作 ( AsyncPattern=true) 使用异步编程模型实现。也就是说,您使用两个异步操作(“BeginOperation”和“EndOeration”)实现一个操作(“Operation”)。Task客户端可以用一个(可能是FromAsync重载)包装这些操作

例如:

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

    int EndDoWork(IAsyncResult result);
}

WCF 合同不返回Task<T>

然后,在客户端上,您可以执行以下操作:

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);

有关更多信息,请参阅:

如果你想用Task<T>,相信你不需要AsyncPattern=true

于 2013-02-22T18:38:01.473 回答