danzuep / MailKitSimplified

Send and receive emails easily, fluently, with one line of code for each operation.
MIT License
80 stars 10 forks source link

MailKit Dependency Injection With Quartz #31

Closed faisalbwn closed 1 year ago

faisalbwn commented 1 year ago

Dear Daniel,

I am injecting the MailKitSimplifiedEmailReceiver in QuartzHostedService and using it in Quartz job, but due to following error Quartz job is not firing again, also the log is not being written:

warn: MailKitSimplified.Receiver.Services.LogFileWriterQueue[0] Log queue cancelled while logs are still being written. fail: Quartz.Core.QuartzSchedulerThread[0] Runtime error occurred in main trigger firing loop. System.ObjectDisposedException: Cannot access a disposed object. Object name: 'LoggerFactory'. at Microsoft.Extensions.Logging.LoggerFactory.CreateLogger(String categoryName) at Quartz.Simpl.MicrosoftLoggingProvider.GetLogger(String name) at Quartz.Logging.LogProvider.GetLogger(String name) at Quartz.Logging.LogProvider.GetLogger(Type type, String fallbackTypeName) at Quartz.Core.JobRunShell..ctor(IScheduler scheduler, TriggerFiredBundle bundle) at Quartz.Impl.StdJobRunShellFactory.CreateJobRunShell(TriggerFiredBundle bndle) at Quartz.Core.QuartzSchedulerThread.Run()

danzuep commented 1 year ago

I thought this might become an issue, but no one had complained about it so I wasn't sure. Here's the reason from Microsoft: "Services resolved from the container should never be disposed by the developer." https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines#disposal-of-services

danzuep commented 1 year ago

Please check the latest preview release.

faisalbwn commented 1 year ago

Thanks Daniel. I updated to pre-release and it fixed the issue. But there is one more issue related to it, I am giving the "ProtocolLog" path in appSetttings.json but it is not creating the log files for both sender and receiver. Can you please guide for sender and receiver logging.

danzuep commented 1 year ago

I'd written my own file writer that was set as follows: "ProtocolLogger:FileWriter:FilePath": "Logs\\ImapClient.txt" I've fixed it in the latest pre-release so you can use the method in the documentation again: "ProtocolLog": "Logs\\ImapClient.txt"

Please check it works and close this issue if it does.

faisalbwn commented 1 year ago

Thanks for helping out Daniel. Closing the ticket and looking forward for stable release.

danzuep commented 1 year ago

Jeffrey Stedfast himself (of MailKit fame) replied to our query with some suggestions on how to improve performance by using UniqueIdSet with UniqueIdRange, so please try out the latest release.