Search code examples
c#outlookconsole-applicationthreadpoolexchangewebservices

The process was terminated due to an unhandled exception. System.Net.HttpWebRequest.GetResponse() exception in ews


Hello everyone i am stuck with this exception and it is stopping my service. please help following is the full stack trace.

Description: The process was terminated due to an unhandled exception. Exception Info: System.Net.WebException at System.Net.HttpWebRequest.GetResponse() at Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest.GetResponse() at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest)

Exception Info: Microsoft.Exchange.WebServices.Data.ServiceRequestException at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest ByRef) at Microsoft.Exchange.WebServices.Data.HangingServiceRequestBase.InternalExecute() at Microsoft.Exchange.WebServices.Data.StreamingSubscriptionConnection.Open() at ERSupportGetOutlookEmail.EmailExchange.OnDisconnect(System.Object, Microsoft.Exchange.WebServices.Data.SubscriptionErrorEventArgs) at Microsoft.Exchange.WebServices.Data.StreamingSubscriptionConnection.InternalOnDisconnect(System.Exception) at Microsoft.Exchange.WebServices.Data.StreamingSubscriptionConnection.OnRequestDisconnect(System.Object, Microsoft.Exchange.WebServices.Data.HangingRequestDisconnectEventArgs) at Microsoft.Exchange.WebServices.Data.HangingServiceRequestBase.InternalOnDisconnect(Microsoft.Exchange.WebServices.Data.HangingRequestDisconnectReason, System.Exception) at Microsoft.Exchange.WebServices.Data.GetStreamingEventsResponse.ReadElementsFromXml(Microsoft.Exchange.WebServices.Data.EwsServiceXmlReader) at Microsoft.Exchange.WebServices.Data.ServiceResponse.LoadFromXml(Microsoft.Exchange.WebServices.Data.EwsServiceXmlReader, System.String) at Microsoft.Exchange.WebServices.Data.GetStreamingEventsRequest.ParseResponse(Microsoft.Exchange.WebServices.Data.EwsServiceXmlReader) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ReadResponse(Microsoft.Exchange.WebServices.Data.EwsServiceXmlReader) at Microsoft.Exchange.WebServices.Data.HangingServiceRequestBase.ParseResponses(System.Object) at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object) at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

Here is my code please look into this

            public async System.Threading.Tasks.Task Watch()
            {
                //Configuration options for a public client application (desktop/mobile app)
                var pcaOptions = new PublicClientApplicationOptions
                {
                    ClientId = ConfigurationManager.AppSettings["appId"],
                    TenantId = ConfigurationManager.AppSettings["tenantId"]
                };

                //Creates a PublicClientApplicationBuilder from a clientID.
                var pca = PublicClientApplicationBuilder
                .CreateWithApplicationOptions(pcaOptions).Build();

                //access permission.
                var ewsScopes = new string[] { "https://outlook.office.com/EWS.AccessAsUser.All" };

                try
                {

                    // Make the interactive token request
                    var authResult = await pca.AcquireTokenByUsernamePassword(ewsScopes, EmailID, ConvertToSecureString(Password)).ExecuteAsync();
                    //var authResult = await pca.AcquireTokenInteractive(ewsScopes).ExecuteAsync();

                    // Configure the ExchangeService with the access token
                    ewsClient = new ExchangeService();
                    ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
                    ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
                   
                    Console.WriteLine("New Email details will display below:-");
                    // Make an EWS call                                
                    CreateStreamingSubscription(); //Creating subscription.               
                }
                catch (MsalException ex)
                {
                    Email.onErrorOccur(ex.Message, "EmailExchange Watch event MsalException", ex.StackTrace);                    
                }
                catch (Exception ex)
                {
                    Email.onErrorOccur(ex.Message, "EmailExchange Watch event", ex.StackTrace);                    
                }

            }

Solution

  • On the code you posted its whats in CreateStreamingSubscription that is causing the exception, its just being caught by the code block you posted which isn't doing any Exchange requests just authentication. Can you give more detail on when it happens eg does the code work for a while and this happens or it just doesn't work? if it just doesn't work may TLS 1.2 so i would suggest including in

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12