title: C# intermittent "The Autodiscover service couldn't be located"
body: <p>Longtime lurker, first time poster.</p>
  
  <p>I've seen a lot of questions about impersonation via Exchange2010 (which is what I'm doing), but everything I've seen so far has to do with invalid code syntax or invalid passwords.  Hopefully someone can help me with my problem, which is a little more odd.  I'm just trying to make an e-mail, get the appointments from another calendar on the same domain, and send the e-mail with those appointments listed.</p>
  
  <p>My code works, but occasionally it will give me the following error:</p>
  
  <pre><code>An unhandled exception of type 'Microsoft.Exchange.WebServices.Data.AutodiscoverLocalException' occurred in Microsoft.Exchange.WebServices.dll
  Additional information: The Autodiscover service couldn't be located.
  </code></pre>
  
  <p>I deliberately removed any error checking from the code and let it crash upon run.  If I try again immediately, it may or may not work.  If it try again in five minutes, same story.  About 75% of the time it works on the first run, and for the life of me I can't figure out what I'm doing wrong (the error is pointing to the RedirectionUrlValidationCallback, which I've also tried to check to debug/test to no avail). I'm checking four different calendars, and this error can happen on any one of them at the same line.  I can't see what's wrong in my code, so it's really frustrating.</p>
  
  <p>First I initialize stuff to prepare the e-mail to send:</p>
  
  <pre><code>using Microsoft.Exchange.WebServices.Data;
  
  const int NUM_APPTS = 10;
  ExchangeService serviceAuth = new ExchangeService(ExchangeVersion.Exchange2010);
  serviceAuth.Credentials = new WebCredentials("me@mydomain.com", "PASSHERE");
  serviceAuth.AutodiscoverUrl("me@mydomain.com", RedirectionUrlValidationCallback);
  </code></pre>
  
  <p>I make the email, etc, then I go check the calendar and try to see the entries:</p>
  
  <pre><code>ExchangeService serviceCALONE = new ExchangeService(ExchangeVersion.Exchange2010);
  serviceCALONE.Credentials = new WebCredentials("CALONE@mydomain.com", "PASSHERE");
  serviceCALONE.AutodiscoverUrl("CALONE@mydomain.com", RedirectionUrlValidationCallback);
  CalendarFolder calendarCALONE = CalendarFolder.Bind(serviceCALONE, WellKnownFolderName.Calendar, new PropertySet());
  CalendarView cViewCALONE = new CalendarView(startDate, endDate, NUM_APPTS);
  cViewCALONE.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);
  FindItemsResults&lt;Appointment&gt; appointmentsCALONE = calendarCALONE.FindAppointments(cViewCALONE);
  </code></pre>
  
  <p>This is the RedirectionURLValidationCallback function that I used from elsewhere on MSDN:</p>
  
  <pre><code>private static bool RedirectionUrlValidationCallback(string redirectionUrl)
  {
      // The default for the validation callback is to reject the URL.
      bool result = true;
      // This was changed to default to true because an error started appearing with:
      // "The autodiscover service could not be located"
  
      Uri redirectionUri = new Uri(redirectionUrl);
  
      if (redirectionUri.Scheme == "https")
      {
          result = true;
      }
      return result;
  }
  </code></pre>
  
  <p>As you can see (other than the fact that I comment tons to myself as I learn), I tried to return true regardless.  It doesn't seem to change anything either way.  </p>
  
  <p>The error can either happen during the e-mail init or the calendar check, but both times iirc it's at the .AutodiscoverUrl line.  I'm running this off a test box hosted via Rackspace, if that matters.</p>
  
  <p>Any help is much appreciated, thanks!</p>