如何在C#中使用EWS将Time out设置为AutodiscoverUrl调用?

时间:2016-06-22 14:05:03

标签: c# exchangewebservices

我正在处理邮件应用程序。我想使用EWS设置登录服务器的超时时间。我已经设置了15秒的超时时间,但有时需要1分钟或更长时间。

System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
System.Net.ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
service.TraceEnabled = true;
service.Credentials = new WebCredentials(email, Pwd);
service.Timeout = 15000;
service.AutodiscoverUrl(email, RedirectionCallback);
//some times it takes 1 minutes to call (1 minute to reach next statement)
EWS_URL = service.Url.ToString();


//check for certificate
private static bool CertificateValidationCallBack(
        object sender,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
    if ((sslPolicyErrors == System.Net.Security.SslPolicyErrors.None))
    {
        entityExchangeServer.isValid = true;
        return true;
    }

    // If there are errors in the certificate chain, look at each error to determine the cause.
    if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
    {
        if (chain != null && chain.ChainStatus != null)
        {
            foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
            {
                if ((certificate.Subject == certificate.Issuer) &&
                   (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
                {
                    // Self-signed certificates with an untrusted root are valid. 
                    return true;
                }
                else
                {
                    if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
                    {
                        // If there are any other errors in the certificate chain, the certificate is invalid,
                        // so the method returns false.
                        return false;
                    }
                }
            }
        }
        // When processing reaches this line, the only errors in the certificate chain are 
        // untrusted root errors for self-signed certificates. These certificates are valid
        // for default Exchange server installations, so return true.
        return true;
    }
    else
    {
        // In all other cases, return false.
        return false;
    }
}

static bool RedirectionCallback(string url)
{
    return true;
}

我已经设置了15秒的超时时间,但有时需要1分钟或更长时间。

有人可以建议如何设定时间吗?

0 个答案:

没有答案