[英]No matter what I try: The I/O operation has been aborted because of either a thread exit or an application request
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.我尝试构建一个简单的 async net tcp wcf 工具,它将打开连接、发送命令、接收答案(包含 0-10 个字符串句子的列表)、关闭连接。
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/O 操作已因线程退出或应用程序请求而中止” ,当然在客户端相应诸如“现有连接已被远程主机关闭”和超时等错误。
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):客户端(在 .NET 4.0 上运行,大约每秒调用一次):
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):服务器端(在 .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?
难道它根本与 WCF 无关吗? 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
System.Net.Sockets.SocketException, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 由于 System.ServiceModel.Channels.SocketConnection 的线程退出或应用程序请求,I/O 操作已中止。 HandleReceiveAsyncCompleted() 在 System.ServiceModel.Channels.SocketConnection.OnReceiveAsync(Object sender, SocketAsyncEventArgs eventArgs) 在 System.Net.Sockets.SocketAsyncEventArgs.FinishOperationAsyncFailure(SocketError socketError, Int32 bytesTransferred, SocketFlags flags) 在 System.Net.Sockets.SocketAsyncEventArgs.CompletionPortCallback (UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped) 在 System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP) System.Net.Sockets.SocketException (0x80004005):I/O 操作已中止,因为线程退出或应用程序请求 3E3
One more interesting fact: SVCLogs show me that the I/O Exeption occurs after a timespan I can define in the另一个有趣的事实:SVCLogs 告诉我 I/O 异常发生在我可以在
<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.
在本例中,它将在 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.
我的解释:由于那里的设置,它会关闭打开的连接,这会导致异常,因为 ReceiveAsync 操作 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?到目前为止,我的问题是为什么 client.close() 没有完全关闭它,为什么它在调用 c_getdatacompleted-event 时还没有完成? Why does the operation "hang out" for 02:39 minutes and does not come to an end?
为什么手术“挂出”到 02:39 分钟还没有结束?
(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) (如果我不通过连接池设置强制关闭,如果我使用 netstat i.ex. 显示,我最终会打开数百个操作)
Async WCF operations ( AsyncPattern=true
) are implemented with the Asynchronous Programming Model.异步 WCF 操作 (
AsyncPattern=true
) 使用异步编程模型实现。 That is, you implement an operation ("Operation") with two asynchronous operations ("BeginOperation" and "EndOeration").也就是说,您使用两个异步操作(“BeginOperation”和“EndOeration”)实现一个操作(“Operation”)。 The client can wrap those operations with a
Task
(presumably with the FromAsync
overload)客户端可以用
Task
包装这些操作(可能是FromAsync
重载)
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>
WCF 合同不返回
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
.如果你想使用
Task<T>
,我相信你不需要AsyncPattern=true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.