4

I'm uploading a C# Console Application as an Azure Webjob. The error I'm getting is:

Unhandled Exception: Microsoft.Exchange.WebServices.Data.AutodiscoverLocalException: The Autodiscover service couldn't be located.

at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.InternalGetLegacyUserSettings[TSettings](String emailAddress, List1 redirectionEmailAddresses, Int32& currentHop)

at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.GetLegacyUserSettings[TSettings](String emailAddress)

at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.InternalGetLegacyUserSettings(String emailAddress, List`1 requestedSettings)

at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.GetUserSettings(String userSmtpAddress, UserSettingName[] userSettingNames)

at Microsoft.Exchange.WebServices.Data.ExchangeService.GetAutodiscoverUrl(String emailAddress, ExchangeVersion requestedServerVersion, AutodiscoverRedirectionUrlValidationCallback validateRedirectionUrlCallback)

at Microsoft.Exchange.WebServices.Data.ExchangeService.AutodiscoverUrl(String emailAddress, AutodiscoverRedirectionUrlValidationCallback validateRedirectionUrlCallback)

Here's My Code:

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@mySite.com", "myPassword", "mysite.com");
    service.AutodiscoverUrl("email@mySite.com", RedirectionUrlValidationCallback);

    // More irrelevant code here
}

The code above was taken from this question as an Accepted Answer: Connection to Office 365 by EWS API

Running this code on my machine as a console application, runs just fine. But this errors out as a webjob, can anyone assist?

Community
  • 1
  • 1
LatentDenis
  • 2,839
  • 12
  • 48
  • 99
  • I had issues using EWS on Azure and after hunting for answers eventually just decided to run the console app on a local server instead and used Task Scheduler to make it run on a set schedule – M Y May 23 '17 at 19:09
  • @hellyale hopefully I can diagnose the issue, but unfortunately I don't have the comfort of resorting to a local server for the procedure. :( – LatentDenis May 23 '17 at 19:12

1 Answers1

4

I tested your code with my Office 365 account, it worked fine on my side. I also use Console.WriteLine to print out the return URL and service URL. Here is what I see in the WebJob dashboard.

[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

Please double check your user name and password. Are they right and Is your password expired?

To get the detail information of failed reason, we could turn on the Application log on your web app Diagnostics logs panel and set TraceEnabled property to true. We can got what was wrong by viewing the application trace logs.

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

In addition, since there is only one EWS endpoint in Office365. We can set the service URL directly instead of use Auto-discover. Code below is for your reference.

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new WebCredentials("user@domain.onmicrosoft.com", "password", "domain.onmicrosoft.com");
service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
Amor
  • 8,325
  • 2
  • 19
  • 21
  • Using the service URL directly worked like a charm! Seems like AutoDiscover was slowing this process down too, since this direct URL process is faster. – LatentDenis May 24 '17 at 12:48