首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MailKit版本2.15.0断开连接问题

MailKit版本2.15.0断开连接问题
EN

Stack Overflow用户
提问于 2021-10-25 17:20:39
回答 1查看 146关注 0票数 1

我使用的是MailKit版本2.15.0。当试图使用DisconnectAsync断开客户端连接时,我将得到以下错误

代码语言:javascript
运行
复制
ClientDisconnect method()==> Exception Message:  The ReadAsync method cannot be called when another read operation is pending., Exception String: System.NotSupportedException:  The ReadAsync method cannot be called when another read operation is pending.
         at System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](TIOAdapter adapter, Memory`1 buffer)
         at MailKit.Net.Pop3.Pop3Stream.ReadAheadAsync(Boolean doAsync, CancellationToken cancellationToken)
         at MailKit.Net.Pop3.Pop3Stream.ReadLineAsync(Stream ostream, Boolean doAsync, CancellationToken cancellationToken)
         at MailKit.Net.Pop3.Pop3Engine.ReadLineAsync(Boolean doAsync, CancellationToken cancellationToken)
         at MailKit.Net.Pop3.Pop3Engine.ReadResponseAsync(Pop3Command pc, Boolean doAsync)
         at MailKit.Net.Pop3.Pop3Engine.IterateAsync(Boolean doAsync)
         at MailKit.Net.Pop3.Pop3Client.SendCommandAsync(Boolean doAsync, CancellationToken token, String command)
         at MailKit.Net.Pop3.Pop3Client.DisconnectAsync(Boolean quit, Boolean doAsync, CancellationToken cancellationToken)

下面是我用来断开客户端连接的代码

代码语言:javascript
运行
复制
private async Task DisconnectClient(Hashtable markAsRead, EmailServer emailServer)
{
    pollingInProgress = false;
    await emailServer.ClientDisconnect(markAsRead);
    markAsRead.Clear();
}
代码语言:javascript
运行
复制
   internal void MarkAsRead(Hashtable markAsRead)
        {
            emailBinding.emailSource().MarkRead(markAsRead);
        }

        internal async Task ClientDisconnect(Hashtable markAsRead)
        {
            try
            {
                if (!emailBinding.emailSource().IsConnected)
                {
                    await emailBinding.Connect();
                }

                FlushClient(markAsRead);

                await emailBinding.Disconnect();
            }
            catch (Exception e)
            {
                AttachWorkerLogs(string.Format("ClientDisconnect method()==> Exception Message: {0}, Exception String: {1}", e.Message, e.ToString()));
                throw;
            }
        }

        private void FlushClient(Hashtable markAsRead)
        {
            MarkAsRead(markAsRead);
        }

最后,像下面这样调用断开方法:

代码语言:javascript
运行
复制
public async Task Disconnect()
        {
            try
            {
                if (client.IsConnected)
                {
                    await client.DisconnectAsync(true);
                }
            }
            catch (Exception e)
            {
                AttachLogger(string.Format("Disconnect method()==> Exception Message: {0}, Exception String: {1}", e.Message, e.ToString()));
                throw;
            }

        }

我不知道到底是什么问题。

EN

回答 1

Stack Overflow用户

发布于 2021-11-01 14:38:38

异常消息非常清楚地表明,您正在尝试同时从多个线程读取SslStream。

由于MailKit不是多线程的,这意味着您实际上是在同时从多个线程对同一个Pop3Client执行两个或更多的操作。

换句话说,问题不在于您正在调用断开连接,而是当您的代码也试图从服务器下载消息或其他东西时,您正在调用断开连接。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69712284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档