背景
公司做Telegram开发,.net Framework项目,调用TLSharp作为框架进行开发。
开发需求是读取群里新到达的信息并进行过滤。
由此不可避免得要用到
TLSharp.Core.TelegramClient.GetHistoryAsync(TLAbsInputPeer peer, int offsetId = 0, int offsetDate = 0, int addOffset = 0, int limit = 100, int maxId = 0, int minId = 0, CancellationToken token = default);
这一方法。
由于每次都只能取得一个群的聊天历史记录,显然地在读取群列表之后第一想到地就是用linq
(await Listener.Client.SendRequestAsync<TLChats>(new TLRequestGetAllChats()
{ ExceptIds = new TeleSharp.TL.TLVector<int>() })
.ConfigureAwait(false))
.Chats
.Where(item => item.GetType() == typeof(TLChannel))
.Cast<TLChannel>()
.ToList()
.ForEach(async item =>
{
((TLChannelMessages)await Listener.Client.GetHistoryAsync(
peer: new TLInputPeerChannel()
{
ChannelId = item.Id,
AccessHash = item.AccessHash.Value
}))
.Messages
.Where(subitem =>
subitem.GetType() == typeof(TLMessage))
.Cast<TLMessage>()
.Where(subitem =>
(subitem.Entities == null
|| (subitem.Entities != null && subitem.Entities.Count() < 5))
&& !string.IsNullOrWhiteSpace(subitem.Message))
.ToList()
.ForEach(subitem =>
{
//实际处理消息
});
});
但是很意外的,跑挂了!
报出的原因是session.dat文件被占用。
探索
session.dat文件是TG的消息会话文件,受TLSharp管控,因此不能自主去管理文件的打开关闭和释放。
于是抱着试一试的心理,把异步去掉了,再跑起来,还是一样的错误。
难道是Linq的问题?还是因为没有加ConfigAwait(false)?
这个框架下试了几次,均报session.dat被占用。
于是用foreach改写了这一段:
List<TLChannel> AllGroups = (await Listener.Client.SendRequestAsync<TLChats>(new TLRequestGetAllChats()
{ ExceptIds = new TeleSharp.TL.TLVector<int>() })
.ConfigureAwait(false))
.Chats
.Where(item => item.GetType() == typeof(TLChannel))
.Cast<TLChannel>()
.ToList();
foreach (TLChannel item in AllGroups)
{
((TLChannelMessages)await Listener.Client.GetHistoryAsync(
peer: new TLInputPeerChannel()
{
ChannelId = item.Id,
AccessHash = item.AccessHash.Value
}))
.Messages
.Where(subitem =>
subitem.GetType() == typeof(TLMessage))
.Cast<TLMessage>()
.Where(subitem =>
(subitem.Entities == null
|| (subitem.Entities != null && subitem.Entities.Count() < 5))
&& !string.IsNullOrWhiteSpace(subitem.Message))
.ToList()
.ForEach(subitem =>
{
//实际处理消息
});
};
继续跑,继续挂!!
然后其实又把foreach改成了for(;