C# ExchangeWeb服务API非常慢,但只有在从WPF应用程序调用时才可以

C# ExchangeWeb服务API非常慢,但只有在从WPF应用程序调用时才可以,c#,wpf,exchangewebservices,C#,Wpf,Exchangewebservices,我正在尝试构建一个WPF应用程序,该应用程序使用Exchange Web服务API查询我们的Exchange 2010 SP2 然而,我发现如果我从WPF应用程序使用ExchangeAPI,对API的调用会非常非常缓慢。事实上,它们比从命令行应用程序运行完全相同的代码慢两个数量级(从WPF应用程序执行某些API调用需要15秒,从命令行应用程序执行这些调用只需要0.15秒) 我正在使用Visual Studio 2015和Exchange Web服务管理的API 2.2 这是WPF的代码。特别是,

我正在尝试构建一个WPF应用程序,该应用程序使用Exchange Web服务API查询我们的Exchange 2010 SP2

然而,我发现如果我从WPF应用程序使用ExchangeAPI,对API的调用会非常非常缓慢。事实上,它们比从命令行应用程序运行完全相同的代码慢两个数量级(从WPF应用程序执行某些API调用需要15秒,从命令行应用程序执行这些调用只需要0.15秒)

我正在使用Visual Studio 2015和Exchange Web服务管理的API 2.2

这是WPF的代码。特别是,运行速度非常慢的是调用
GetPathFolder
中的
folder.FindFolders

using System;
using System.Windows;
using Microsoft.Exchange.WebServices.Data;

namespace esw_testwpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    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;
        }
    }
}
如果代码基本相同,为什么在Exchange API响应时间中会出现如此不同的行为

我最初认为这是某种服务器端节流,但WPF变量总是非常慢,而命令行变量总是非常快,这对我来说毫无意义。

当我更改时:

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;
        }
    }
}
service.TraceEnabled = true;


WPF中所有额外的延迟都消失了。因此,似乎出于某种原因,在WPF中启用EWS跟踪会带来巨大的性能损失。

您是否尝试过发布这两个版本的版本,并在没有附加调试程序的情况下运行它?还是比较慢吗?是的,发布版本的构建大约需要相同的时间。WPF版本从开始到结束需要26-27秒,而命令行版本大约需要1秒to
service.TraceEnabled=false所有额外延迟消失。由于某些原因,EWS跟踪对WPF影响很大。
service.TraceEnabled = false;