-
-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Monitor multiple boxes threads issue #63
Comments
Hi @Sylar-A, glad you found the FolderMonitor feature as I'm not sure I'd documented it fully 😊. I ran it in my test environment but the project I initially wanted to use it for has been and gone, so I haven't actually used it in a real-world setting yet. It sounds like you've figured out the fix for this issue though, so well done! Please make a fork of the project, test your proposed changes on your machine, then make a pull request to merge your changes into the main branch. Then your name will appear on the main page as a contributor 🙂 |
Hi, @danzuep! |
As a short-term workaround you could try using the ImapReceiverFactory instead. If that works then using the copy function could be the solution. |
Hi, @danzuep! If i understand you correct, it must be a BackgroundService with while loop and with some timeout?
But i didn't realize how to use the copy method as you suggested. |
The method copy is used to ensure each receiver has a unique IMAP client, but it's used inside the receiver factory and the idle client, so that should be enough. You could try adding Task.WhenAll with mailbox monitors to your code above, or try modifying the monitor factory directly to find the source of the error. Enable debug logging to see further into the stack, breakpoints are good too but harder to follow with all the multi-threading. I won't be able to do this myself for another week or so, much better for both of us if you can find the issue before then 🙂 |
Oh, ok, Daniel, thanks for parting words, i'll try to hard debug it😅 |
Looking back at the error message, it says the "IMAP client is being accessed by multiple threads." The IMAP client isn't being passed through when the MailFolderMonitor is constructed though. Then I saw the second part that says "GetBodyTextAsync(IMessageSummary)". The short term fix then is not to use |
Hi, @danzuep! I have found another issue with IImapReceiverFactory. When it's creates new receivers and all of them have same ImapHost, it's takes only one mail (in my case one same mail seven times instead of 7 different mails). When I debugging it, the problem was in IMemoryCache extension method GetOrCreate (from Microsoft). Because of all mails have same ImapHost, it wasn't creates new item and tooks the same item 7 times by the key. It was here. |
Wow, good find! |
Ok, Daniel. I could do it later. |
Hi, @danzuep! Scoped services would be great! |
The answer to this issue you've found is to not use use the messageSummary that comes from the MailFolderMonitor or the messageSummary.Folder. I've added a MailFolderCache function you can use from the ImapReceiver, so use the UniqueId and the GetMailFolderAsync() function instead of using the MessageSummary directly. One possible solution would be for me to just return the UniqueId instead of returning the MessageSummary. |
Here's a sample using the latest pre-release: using var scope = _serviceScopeFactory.CreateScope();
var mailFolderClient = scope.ServiceProvider.GetRequiredService<IMailFolderClient>();
var mailFolderMonitorFactory = scope.ServiceProvider.GetRequiredService<IMailFolderMonitorFactory>();
async Task UniqueIdArrivedAsync(IMessageSummary messageSummary) =>
await mailFolderClient.MoveToAsync(messageSummary, destinationFolderFullName, cancellationToken);
await mailFolderMonitorFactory.MonitorAllMailboxesAsync(UniqueIdArrivedAsync, cancellationToken); |
Please test the changes and let me know how it goes. |
Hi, @danzuep! |
Ah. it seems that GitVersion_NuGetVersion didn't update, even though the semantic version is correct. Weird, that used to work. I re-ran it with a higher version and it published this time. |
Hi, @danzuep!
Somewhere here (deeper in MailKit):
|
It sounds like the mail folder name is not found. What do the protocol logs say? Do the folder names match? |
ProtocolLog is empty. I didn't change the folder name. It's same name that i pass to |
Some of the issues you mentioned are fixed in the latest pre-release, try updating. The main thing I haven't tried is creating folders, but the other ones should work now. The root of the issue was mistakenly using the IMAP client directly without connecting or authenticating it first. |
Hi, Daniel! |
Looks like it was there when you commented, but just in case I've made a new one. Please do a Pull Request if you notice any issues. |
It seems it's a problem with your authentication settings, but the issue isn't clear from the error message ( |
It says
|
"Application password is REQUIRED" (Gmail has the same requirement).
|
Hi, @danzuep!
|
Hi @danzuep! |
I haven't been able to replicate the issue you're having sorry. Have you tried connecting to GMail or one of the other big providers? Try downloading the source code and using breakpoints to pinpoint the issue. |
Hi, @danzuep!
At first, thanks a lot for the monitoring multiple boxes feature!
At second, there is some issue with it.
I have registered 7 FolderMonitors in appsettings and using IMailFolderMonitorFactory.MonitorAllMailboxesAsync method to monitor them. So, for example (and not only this example), when i get 2 mail in 1 mailbox same time, it's thows exceptions:
At third, there is example with worker which is Singleton and have injected from constructor dependency on IImapReceiver, which is Transient. So, it won't be transient, WorkerService will control it's lifetime, and it would be Singleton. There is recomendation from Microcoft to do it rigth way.
Exceptions throws every day several times😥
Please, help, Daniel🙏
The text was updated successfully, but these errors were encountered: