C# 我们需要检查 Exchange 上的哪些设置以避免抛出 ServiceRequestException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14417516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:44:09  来源:igfitidea点击:

What settings on Exchange do we need to check to avoid a ServiceRequestException from being thrown?

c#exchangewebservicesexchange-server-2010ews-managed-api

提问by Yohan Seimon

We're programmatically (using the Microsoft Exchange Web Services Managed API 2.0) to access the user's calendar (on Exchange 2010 SP1).

我们以编程方式(使用 Microsoft Exchange Web Services Managed API 2.0)访问用户的日历(在 Exchange 2010 SP1 上)。

We have been able to successfully communicate with EWS via auto-discovery in our development environment on which we didn't have to make any specific settings (we used the default out-of-the-box settings) on Exchange.

我们已经能够在我们的开发环境中通过自动发现成功地与 EWS 通信,我们不必在 Exchange 上进行任何特定设置(我们使用默认的开箱即用设置)。

Unfortunately the same doesn't work on the client's environment. The client doesn't have a test environment. We're supposed to communicate to their live Exchange server.

不幸的是,这同样不适用于客户端的环境。客户端没有测试环境。我们应该与他们的实时 Exchange 服务器进行通信。

Initially auto-discovery didn't work on the client's environment. We got the following error:

最初自动发现在客户端的环境中不起作用。我们收到以下错误:

Microsoft.Exchange.WebServices.Data.AutodiscoverLocalException: The Autodiscover service couldn't be located.
   at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.InternalGetLegacyUserSettings[TSettings](String emailAddress, List`1 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)

So now we explicitly specify the URL to the EWS. This gave us the following error:

所以现在我们明确指定 EWS 的 URL。这给了我们以下错误:

Microsoft.Exchange.WebServices.Data.ServiceRequestException: The request failed. The remote server returned an error: (401) Unauthorized. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized.
       at System.Net.HttpWebRequest.GetResponse()
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(HttpWebRequest request)
       --- End of inner exception stack trace ---
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(HttpWebRequest request)
       at Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.InternalExecute()
       at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
       at Microsoft.Exchange.WebServices.Data.ExchangeService.BindToFolder(FolderId folderId, PropertySet propertySet)
       at Microsoft.Exchange.WebServices.Data.ExchangeService.BindToFolder[TFolder](FolderId folderId, PropertySet propertySet)
       at Microsoft.Exchange.WebServices.Data.CalendarFolder.Bind(ExchangeService service, FolderId id)

The exception is thrown in line 5 of the following code:

在以下代码的第 5 行抛出异常:

ServicePointManager.ServerCertificateValidationCallback = this.CertificateValidationCallBack;
ExchangeService exchangeWebService = new ExchangeService(ExchangeVersion.Exchange2010_SP1);

exchangeWebService.Credentials = new WebCredentials("[email protected]", "myPassword");
exchangeWebService.AutodiscoverUrl("[email protected]", this.RedirectionUrlValidationCallback);

**CalendarFolder calendarFolder = CalendarFolder.Bind(exchangeWebService, new FolderId(WellKnownFolderName.Calendar, userName));**

CalendarView calendarView = new CalendarView(startDate, endDate);
calendarView.PropertySet = new PropertySet(BasePropertySet.IdOnly, AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.IsRecurring, AppointmentSchema.AppointmentType, AppointmentSchema.End, AppointmentSchema.Duration);

FindItemsResults<Appointment> findResults = calendarFolder.FindAppointments(calendarView);

We don't mind that auto-discovery doesn't work since we can explicitly specify the URL to the EWS. What we would like to know are what settings, permissions etc. we need to check on the client's instance of Exchange to determine why the above exception (ServiceRequestException) is being thrown.

我们不介意自动发现不起作用,因为我们可以明确指定 EWS 的 URL。我们想知道的是哪些设置、权限等我们需要检查客户端的 Exchange 实例以确定为什么会抛出上述异常 (ServiceRequestException)。

We have requested the following commands to be executed on the Exchange Management Shell on the client's instance of Exchange:

我们已请求在 Exchange 客户端实例的 Exchange 命令行管理程序上执行以下命令:

Test-OutlookWebServices –Identity username
Get-OrganizationConfig

We are yet to receive the results. Please let us know if there is anything else we should check.

我们还没有收到结果。如果还有什么我们应该检查的,请告诉我们。

采纳答案by Yohan Seimon

We knew that the client had a proxy address. What we didn't know is that we needed to use the domain address ([email protected]) when authenticating with EWS and the proxy address ([email protected]) when invoking AutodiscoverUrl and when accessing the mailbox.

我们知道客户端有一个代理地址。我们不知道的是,在使用 EWS 进行身份验证时需要使用域地址 ([email protected]),在调用 AutodiscoverUrl 和访问邮箱时需要使用代理地址 ([email protected])。

So the above code should be:

所以上面的代码应该是:

ServicePointManager.ServerCertificateValidationCallback = this.CertificateValidationCallBack;
ExchangeService exchangeWebService = new ExchangeService(ExchangeVersion.Exchange2010_SP1);

exchangeWebService.Credentials = new WebCredentials("[email protected]", "myPassword");
exchangeWebService.AutodiscoverUrl("[email protected]", this.RedirectionUrlValidationCallback);

CalendarFolder calendarFolder = CalendarFolder.Bind(exchangeWebService, new FolderId(WellKnownFolderName.Calendar, "[email protected]"));

We were using either the domain address or the proxy address. We never tried the above combination, until we accidentally forgot to change the one on line #3 of the above code. :)

我们使用的是域地址或代理地址。我们从未尝试过上述组合,直到我们不小心忘记更改上述代码第 3 行的组合。:)

Thank you, to everyone that viewed the question and tried to answer it. I hope this will help someone someday.

谢谢所有看过问题并试图回答的人。我希望有一天这会对某人有所帮助。

回答by nitesh.kodle123

I will recommend you to enable Traces,to achieve this follow :

我会建议您启用跟踪,以实现以下目标:

 Service.TraceEnabled = true;

I was facing the same issue then when I enabled traces these traces will guide you what exactly is happening.In my case SSL certificate issue is there to solve it i followed following post

我遇到了同样的问题,然后当我启用跟踪时,这些跟踪将指导您到底发生了什么。在我的情况下,SSL 证书问题可以解决它,我遵循以下帖子

There can be many issue such as:

可能有很多问题,例如:

  • User can be blocked.

  • The DSN can't find autodiscover.domain.com

  • 用户可以被阻止。

  • DSN 找不到 autodiscover.domain.com