我有一个异步处理套接字通信的程序。我得到的异常只发生在发布版本中(在构建机器上)。导致问题的代码是开始监听传入套接字连接的非常简单的例程:
public async Task Listen()
{
try
{
var endpoint = new IPEndPoint(IPAddress.Loopback, Port);
using (Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
Socket.Bind(endpoint);
Socket.Listen(Limit);
while (!Shutdown)
{
var socket = await Socket.AcceptAsync().ConfigureAwait(false);
// some code handling connection
var result = await Socket.ReceiveAsync(state).ConfigureAwait(false);
Received(result, state);
}
}
}
catch (ObjectDisposedException) {}
catch (Exception ex) when (ex is SocketException || ex is ApplicationException || ex is ThreadAbortException)
{
OnError?.Invoke(this, ex);
Dispose();
}
}
AcceptAsync 和 ReceiveAsync 是使用 TPL 模式和 Task.Factory.FromAsync 的扩展方法。确切的例外如下:
Exception Type: System.InvalidProgramException
Exception Message: Common Language Runtime detected an invalid program.
这似乎发生在:
System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start
调用此方法时会立即生成异常。有什么想法可能是错的吗?