简体   繁体   English

C#间歇性“找不到自动发现服务”

[英]C# intermittent “The Autodiscover service couldn't be located”

Longtime lurker, first time poster. 长期潜伏,第一次海报。

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. 我已经看到很多有关通过Exchange2010模拟的问题(这是我正在做的事情),但是到目前为止,我所看到的一切都与无效的代码语法或无效的密码有关。 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. 我只是想发送一封电子邮件,从同一域的另一个日历中获取约会,然后发送列出了这些约会的电子邮件。

My code works, but occasionally it will give me the following error: 我的代码有效,但偶尔会出现以下错误:

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.

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). 大约有75%的时间在第一次运行时有效,而且对我而言,我无法弄清楚自己在做错什么(错误指向RedirectionUrlValidationCallback,我也尝试对其进行调试检查) /测试无济于事)。 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. 我看不到我的代码有什么问题,所以这真令人沮丧。

First I initialize stuff to prepare the e-mail to send: 首先,我初始化东西以准备发送电子邮件:

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);

I make the email, etc, then I go check the calendar and try to see the entries: 我写了电子邮件,等等,然后去检查日历并尝试查看条目:

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<Appointment> appointmentsCALONE = calendarCALONE.FindAppointments(cViewCALONE);

This is the RedirectionURLValidationCallback function that I used from elsewhere on MSDN: 这是我在MSDN上其他地方使用的RedirectionURLValidationCallback函数:

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;
}

As you can see (other than the fact that I comment tons to myself as I learn), I tried to return true regardless. 如您所见(除了我在学习过程中对自己发表评论的事实之外),无论如何我都尝试返回true。 It doesn't seem to change anything either way. 似乎没有任何改变的方式。

The error can either happen during the e-mail init or the calendar check, but both times iirc it's at the .AutodiscoverUrl line. 该错误可能在电子邮件初始化或日历检查期间发生,但两次都在.AutodiscoverUrl行上。 I'm running this off a test box hosted via Rackspace, if that matters. 如果重要的话,我正在通过Rackspace托管的测试盒上运行它。

Any help is much appreciated, thanks! 非常感谢任何帮助,谢谢!

Try using Service.Credentials in this format: 尝试使用以下格式的Service.Credentials

Service.Credentials = new WebCredentials(username, password, domainname);

Also, check if the password for the email address has expired. 另外,检查电子邮件地址的密码是否已过期。 If the password has expired you will get this error from Autodiscover. 如果密码已过期,您将从自动发现中收到此错误。

Another troubleshooting option to test Autodiscover externally is to use Microsoft Remote Connectivity Analyser . 外部测试自动发现的另一种故障排除选项是使用Microsoft远程连接分析器

If all else fails I would recommend enabling Tracing to help troubleshoot the issue. 如果所有其他方法均失败,建议您启用跟踪以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM