title: Exchange Web Service API really slow but only if called from WPF application
body: <p>I am trying to build a WPF application that queries our Exchange 2010 SP2 using the Exchange Web Services API.</p>
  
  <p>However, I'm finding that if I use the Exchange API from a WPF application, the calls to the API are really, really slow. In fact they are two orders of magnitude slower than running the exact same code from a command-line application (some calls to the API takes 15 seconds when done from a WPF app and only 0.15 seconds when done from a command-line app).</p>
  
  <p>I'm using Visual Studio 2015 with Exchange Web Services Managed API 2.2.</p>
  
  <p>This is the code for WPF. In particular, what runs extremely slow is the call to <code>folder.FindFolders</code> in <code>GetPathFolder</code>:</p>
  
  <pre><code>using System;
  using System.Windows;
  using Microsoft.Exchange.WebServices.Data;
  
  namespace esw_testwpf
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
              var service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
              service.UseDefaultCredentials = true;
              service.TraceEnabled = true;
              service.TraceFlags = TraceFlags.All;
              service.AutodiscoverUrl("myemail@company.com", RedirectionUrlValidationCallback);
  
              Folder baseFolder = FindFolderIdByPath(service, @"\Path\To\A\Known\PublicFolder", WellKnownFolderName.PublicFoldersRoot);
          }
  
          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;
          }
  
          public static Folder FindFolderIdByPath(ExchangeService service, string path, WellKnownFolderName root)
          {
              // Specify the root folder to be searched.
              Folder rootFolder = Folder.Bind(service, root);
  
              return GetPathFolder(rootFolder.FindFolders(new FolderView(100)), path, "");
          }
  
          public static Folder GetPathFolder(FindFoldersResults results, string lookupPath, string currentPath)
          {
              foreach (Folder folder in results)
              {
                  string path = currentPath + @"\" + folder.DisplayName;
                  if (lookupPath.Equals(path))
                  {
                      return folder;
                  }
  
                  if (lookupPath.StartsWith(path))
                  {
                      return GetPathFolder(folder.FindFolders(new FolderView(100)), lookupPath, path);
                  }
                  else
                  {
                      continue;
                  }
              }
              return null;
          }
      }
  }
  </code></pre>
  
  <p>And this exactly the same code for command-line, which consistently runs very fast.</p>
  
  <pre><code>using System;
  using Microsoft.Exchange.WebServices.Data;
  
  namespace ewstestcmd
  {
      class Program
      {
          static void Main(string[] args)
          {
              var service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
              service.UseDefaultCredentials = true;
              service.TraceEnabled = true;
              service.TraceFlags = TraceFlags.All;
              service.AutodiscoverUrl("myemail@company.com", RedirectionUrlValidationCallback);
  
              Folder baseFolder = FindFolderIdByPath(service, @"\Path\To\A\Known\PublicFolder", WellKnownFolderName.PublicFoldersRoot);
          }
  
          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;
          }
  
          public static Folder FindFolderIdByPath(ExchangeService service, string path, WellKnownFolderName root)
          {
              // Specify the root folder to be searched.
              Folder rootFolder = Folder.Bind(service, root);
  
              return GetPathFolder(rootFolder.FindFolders(new FolderView(100)), path, "");
          }
  
          public static Folder GetPathFolder(FindFoldersResults results, string lookupPath, string currentPath)
          {
              foreach (Folder folder in results)
              {
                  string path = currentPath + @"\" + folder.DisplayName;
                  if (lookupPath.Equals(path))
                  {
                      return folder;
                  }
  
                  if (lookupPath.StartsWith(path))
                  {
                      return GetPathFolder(folder.FindFolders(new FolderView(100)), lookupPath, path);
                  }
                  else
                  {
                      continue;
                  }
              }
              return null;
          }
      }
  }
  </code></pre>
  
  <p>Why do I get such a different behavior in Exchange API response time if the code is essentially the same?  </p>
  
  <p>I first thought it was some kind of server-side throttling, but it does not make sense to me that the WPF variant is always very slow and the command-line variant is always very fast.</p>