简体   繁体   中英

Microsoft Exchange oAuth - System.OutOfMemoryException

I've written a console app thats running as a service (using TopShelf) and using a while loop it continuously polls an office 365 inbox every 30 seconds to check for new messages. I'm doing this using oAuth and the below Microsoft libary

Microsoft.Exchange.WebServices 2.2.0.

I'm using the reccomended approach from MS to get the Access Token silently AcquireTokenByUsernamePassword & AcquireTokenSilent.

After about 5 days of running perfectly I'm getting the below exception form Microsoft library:

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at System.String.Concat(String str0, String str1)
   at Microsoft.Exchange.WebServices.Data.OAuthCredentials..ctor(String token, Boolean verbatim) in \\REDMOND\EXCHANGE\BUILD\E15\15.00.0913.015\SOURCES\sources\dev\EwsManagedApi\src\EwsManagedApi\Credentials\OAuthCredentials.cs:line 79
   at Microsoft.Exchange.WebServices.Data.OAuthCredentials..ctor(String token) in \\REDMOND\EXCHANGE\BUILD\E15\15.00.0913.015\SOURCES\sources\dev\EwsManagedApi\src\EwsManagedApi\Credentials\OAuthCredentials.cs:line 36

When I've traced this through its being generated by this section in my code:

                           exchangeService = new ExchangeService
                            {
                                Credentials = new OAuthCredentials(authResult.AccessToken),
                                Url = new Uri(rdr.O365ServiceURL)

                            };

I've got my exchangeService object declared outside of my while loop and then I'm instantiating it inside the loop so I can make use of the AcquireTokenSilent call, otherwise Microsoft refuse the connection and issue the below message back:

Error: Your app has been throttled by AAD due to too many requests. To avoid this, cache your tokens see https://aka.ms/msal-net-throttling.

Is this a bug in Microsoft code or can I do something better to manage the memory here?

Also the service that crashed out was upto 3.6gb memory footprint which is about 3.5gb too high.

There was nothing wrong with the code in question. Whilst not ideal (I'm going to look I to the notification stream approach instead of polling thanks @paulsm4) it still worked and the Exchange Service was not disposable, so could not be instantiated with a using block.

The issue was actually in the log.net framework where I had the memory appender enabled. I removed this from my logger setup and the memory footprint stays where it should be.

This project uses three separate loggers and they were all building up in memory even after it was written to the file.

The instantiating of the exchange object was just the straw that broke the camels back and a red herring.

The app is sitting at a health 14MB memory footprint now.

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM