2013-05-02 10 views
6

Korzystanie z Windows 8 (64 bit) i Visual Studio 2012 Ultimate Aktualizacja 2.Korzystanie EWS z Office 365 nie powiedzie się z serwer nie obsługuje żądanie wersję

Próbuję użyć Exchange Web Services 2.0 (EWS) z biuro 365. otrzymuję następujący wyjątek, który pojawia się na wezwanie do findResults (ostatni wiersz w główny):

Microsoft.Exchange.WebServices.Data.ServiceVersionException was unhandled 
    HResult=-2146233088 
    Message=Exchange Server doesn't support the requested version. 
    Source=Microsoft.Exchange.WebServices 
    StackTrace: 
     at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ProcessWebException(WebException webException) 
     at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request) 
     at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request) 
     at Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.InternalExecute() 
     at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute() 
     at Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems[TItem](IEnumerable`1 parentFolderIds, SearchFilter searchFilter, String queryString, ViewBase view, Grouping groupBy, ServiceErrorHandling errorHandlingMode) 
     at Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems(FolderId parentFolderId, SearchFilter searchFilter, ViewBase view) 
     at Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems(WellKnownFolderName parentFolderName, ViewBase view) 
     at Test_EWS.Program.Main(String[] args) in c:\Users\john.tarbox\Documents\Visual Studio 2012\Projects\Test_EWS\Test_EWS\Program.cs:line 85 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 

Oto mój przykładowy kod:

using Microsoft.Exchange.WebServices.Autodiscover; 
using Microsoft.Exchange.WebServices.Data; 
using System; 
using System.Net; 


namespace Test_EWS 
{ 
    class Program 
    { 
     private static bool CertificateValidationCallBack(
      object sender, 
      System.Security.Cryptography.X509Certificates.X509Certificate certificate, 
      System.Security.Cryptography.X509Certificates.X509Chain chain, 
      System.Net.Security.SslPolicyErrors sslPolicyErrors) 
     { 
      // If the certificate is a valid, signed certificate, return true. 
      if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None) 
      { 
       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. 
          continue; 
         } 
         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 void Main(string[] args) 
     { 
      ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack; 
      ExchangeService service = new ExchangeService(); 
      service.Credentials = new WebCredentials("[email protected]", "password"); 

      service.TraceEnabled = true; 
      service.TraceFlags = TraceFlags.All; 

      try 
      { 
       service.AutodiscoverUrl("[email protected]", RedirectionUrlValidationCallback); 
      } 
      catch (AutodiscoverRemoteException ex) 
      {     
       Console.WriteLine("Exception thrown: " + ex.Error.Message); 

     } 

      FindItemsResults<Item> findResults = service.FindItems(
             WellKnownFolderName.Inbox, 
             new ItemView(10)); 
     } 

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

    } 
} 

Odpowiedz

12

Gdybym zmienić linię:

ExchangeService service = new ExchangeService(); 

do

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); 

kod działa dobrze. Nie rozumiem, dlaczego trzeba wyraźnie przekazać wersję tego zaproszenia; dlaczego nie działa bez konkretnej wersji?

+0

Domyślnie usługa jest ustawiana na najnowszą wersję, która jest obecnie ExchangeVersion.Exchange2013. – Spawnrider

0

Miałem ten sam numer VS2010. W programie Outlook 2013 jest dostępny EWS V15 (GAC \ Microsoft.Exchange.WebServices \ 15.0.0.0 _...), który nie obsługuje Excnage2007_SP1.

Zamiast tego należy użyć programu ExchangeVersion.Exchange2010_SP2. To rozwiązało mój problem.

Powiązane problemy