Intermittent NullReferenceException in EWS Nuget Package

Ihor Kuliiev 156 Reputation points
2024-07-22T08:31:04.09+00:00

Hello!

In my .NET application I use Microsoft.Exchange.WebServices 2.2.0 package to interact with the Exchange Server.

I create the ExchangeService class and call the method:

Folder.Bind(exchangeService, WellKnownFolderName.Inbox).

Most of the time the call is successful. But once in a while I am getting an exception:

System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.WrapStream(Stream responseStream, String contentEncoding) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetResponseStream(IEwsHttpWebResponse response) at Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.ReadResponse(IEwsHttpWebResponse response) at Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.InternalExecute() at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute() at Microsoft.Exchange.WebServices.Data.ExchangeService.BindToFolder(FolderId folderId, PropertySet propertySet) at Microsoft.Exchange.WebServices.Data.ExchangeService.BindToFolder[TFolder] at Microsoft.Exchange.WebServices.Data.Folder.Bind(ExchangeService service, FolderId id, PropertySet propertySet) at Microsoft.Exchange.WebServices.Data.Folder.Bind(ExchangeService service, WellKnownFolderName name)...

The exception occurs sporadically.

Does anyone know how to tackle this issue?

Thank you in advance!

Microsoft Exchange Online
Exchange Server
Exchange Server
A family of Microsoft client/server messaging and collaboration software.
1,337 questions
{count} votes

Accepted answer
  1. Xintao Qiao-MSFT 3,995 Reputation points Microsoft Vendor
    2024-07-23T01:35:47.2933333+00:00

    Hi, @Ihor Kuliiev

    Great to know that the issue has already been resolved and thanks for sharing the solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "[The question author cannot accept their own answer. They can only accept answers by others Accept answers on Microsoft Q&A | Microsoft Learn]", I'll repost your solution in case you'd like to "[Accept]" the answer : )     

    --------------   

    Issue Symptom: 

    Intermittent NullReferenceException in EWS Nuget Package

    Resolution: 

    The solution was built on ASP.NET Core 3.1. After updating to .NET 8.0 the exception was gone.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ihor Kuliiev 156 Reputation points
    2024-07-22T12:24:16.7233333+00:00

    I've found out how to resolve the problem. The solution was built on ASP.NET Core 3.1.

    After updating to .NET 8.0 the exception was gone.

    However, the same code in another application (WPF) with much older .NET version 4.6.1 works smoothly.

    Hope this information will be helpful to somebody.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.