该进程由于未处理的异常而终止。 ews中的System.Net.HttpWebRequest.GetResponse()异常

时间:2020-07-16 08:22:29

标签: c# outlook console-application threadpool exchangewebservices

大家好,我对此异常感到困惑,它正在停止我的服务。请帮助以下是完整的堆栈跟踪。

说明:由于未处理的异常,进程已终止。 异常信息:System.Net.HttpWebRequest.GetResponse()上的System.Net.WebException 在 Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest.GetResponse() 在Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest)

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

这是我的代码,请仔细阅读

            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);                    
                }

            }

1 个答案:

答案 0 :(得分:0)

在代码上,您在导致异常的CreateStreamingSubscription中发布了内容,它只是被您发布的代码块所捕获,该代码块不执行任何Exchange请求而只是进行身份验证。您能否提供有关发生时间的更多详细信息,例如,代码会在一段时间内起作用并且会发生还是根本不起作用?如果它不起作用,可能是TLS 1.2,所以我建议在其中添加

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12