我正在将C#控制台应用程序上传为Azure Webjob。我得到的错误是:


  未处理的异常:
  Microsoft.Exchange.WebServices.Data.AutodiscoverLocalException:
  找不到自动发现服务。
  
  在Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.InternalGetLegacyUserSettings [TSettings](String
  emailAddress,List1重定向,EmailAddresses,Int32&currentHop)
  
  在Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.GetLegacyUserSettings [TSettings](String
  电子邮件地址)
  
  在Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.InternalGetLegacyUserSettings(String
  emailAddress,列出1个请求的设置)
  
  在Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.GetUserSettings(字符串
  userSmtpAddress,UserSettingName [] userSettingNames)
  
  在Microsoft.Exchange.WebServices.Data.ExchangeService.GetAutodiscoverUrl(String
  emailAddress,ExchangeVersion请求的ServerVersion,
  AutodiscoverRedirectionUrlValidationCallback
  validateRedirectionUrlCallback)
  
  在
  Microsoft.Exchange.WebServices.Data.ExchangeService.AutodiscoverUrl(String
  emailAddress,AutodiscoverRedirectionUrlValidationCallback
  validateRedirectionUrlCallback)


这是我的代码:

private static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
    // The default for the validation callback is to reject the URL.
    bool result = false;

    Uri redirectionUri = new Uri(redirectionUrl);

    // Validate the contents of the redirection URL. In this simple validation
    // callback, the redirection URL is considered valid if it is using HTTPS
    // to encrypt the authentication credentials.
    if (redirectionUri.Scheme == "https")
    {
        result = true;
    }
    return result;
}
static void Main(string[] args)
{
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);

    service.Credentials = new WebCredentials("[email protected]", "myPassword", "mysite.com");
    service.AutodiscoverUrl("[email protected]", RedirectionUrlValidationCallback);

    // More irrelevant code here
}


上面的代码从该问题中获取,作为可接受的答案:Connection to Office 365 by EWS API

在我的机器上作为控制台应用程序运行此代码,即可正常运行。但这是网络工作的错误,任何人都可以协助吗?

最佳答案

我使用我的Office 365帐户测试了您的代码,它对我而言很好用。我还使用Console.WriteLine打印出返回URL和服务URL。这是我在WebJob仪表板中看到的内容。

[05/24/2017 05:54:52 > 7adbf1: SYS INFO] Run script 'TestO365WebJob.exe' with script host - 'WindowsScriptHost'
[05/24/2017 05:54:52 > 7adbf1: SYS INFO] Status changed to Running
[05/24/2017 05:54:59 > 7adbf1: INFO] return URL: https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml
[05/24/2017 05:55:00 > 7adbf1: INFO] Service URL https://outlook.office365.com/EWS/Exchange.asmx


请仔细检查您的用户名和密码。没错,您的密码是否过期?

要获取失败原因的详细信息,我们可以打开Web应用程序“诊断日志”面板上的“应用程序日志”,并将TraceEnabled属性设置为true。通过查看应用程序跟踪日志,我们可以找出问题所在。

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.TraceEnabled = true;


此外,由于Office365中只有一个EWS终结点。我们可以直接设置服务URL,而不是使用自动发现。以下代码供您参考。

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new WebCredentials("[email protected]", "password", "domain.onmicrosoft.com");
service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

08-15 22:28