none
[E2010] [EWS] [C#] [Windows]: Testing connection before calling FindItems RRS feed

  • Question

  • Hello,

    I read somewhere that when you call the FindItems method, that it opens and closes the connection for you. Is there a way to verify that the exchange server(s) are up and ready to respond to calls before calling that method?

    I have a simple service that manages a mailbox, and there was maintenance done last night, which resulted in a bunch of connection errors, so I want to try and avoid this, without having to stop the service during maintenance.


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet

    Tuesday, August 26, 2014 3:35 PM

Answers

  • To do HEAD request you can just use something simple like

                HttpWebRequest WRequest;
                HttpWebResponse WResponse;
                WRequest = (HttpWebRequest)HttpWebRequest.Create(service.Url);
                System.Net.CredentialCache credentialCache = new System.Net.CredentialCache();
                credentialCache.Add(service.Url, "Basic", ncCred);
                WRequest.Credentials = credentialCache;
                WRequest.PreAuthenticate = true;
                WRequest.KeepAlive = true;            
                WRequest.CookieContainer = new CookieContainer();
                WRequest.UserAgent = "Test Agent";
                WRequest.Method = "HEAD";
                WRequest.Headers.Add("Accept-Language:" + System.Globalization.CultureInfo.CurrentCulture.Name);
                try
                {
                    WResponse = (HttpWebResponse)WRequest.GetResponse();
                    WResponse.Close();
                }
                catch
                {
                   //process error 
                }

    With processing errors you should be doing it already depending on what method your using to access EWS eg If your using the ManagedAPI it will generated specific exceptions depending on what has gone wrong eg http://msdn.microsoft.com/en-us/library/office/microsoft.exchange.webservices.data.serviceresponseexception(v=exchg.80).aspx and also the ServiceRequestException http://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.servicerequestexception(v=exchg.80).aspx . So you should catch these exceptions in a normal TryCatch work out what happens and then develop your own retry/warning logic.

    Cheers
    Glen

    • Marked as answer by clayman2 Monday, September 15, 2014 1:07 PM
    Monday, September 8, 2014 7:37 AM

All replies

  • >>I read somewhere that when you call the FindItems method, that it opens and closes the connection for you

    EWS uses standard Internet protocols to communicate between the client and server eg any EWS request you send is done as a http POST (posting SOAP over https). So you will have a connection open and closed on each request (like any Web Request). You could make HEAD request to the Exchange.asmx to make sure the communication path is open which would be pretty lightweight.

     Otherwise no matter if your making a FindItem or any other EWS request for instance if your Mailbox Role server is applying the lastest service pack and its dismounted a particular store your CAS server maybe up (responding to pings and EWS maybe responding if you have multiples servers) but the mailbox may not availble.  The best way to deal with this is process the result of every request you make to EWS and create some retry logic with a delay. You should deal with client side issue eg TimeOut, DNS failiure which will generate an exception and Server Side (eg credentials, Stores being dismounted, throttling) via the Service Response.  Each error needs different handling eg if your getting credentials issue you shouldn't really keep trying as you'll lock the account out etc.

    Cheers
    Glen

    Wednesday, August 27, 2014 4:54 AM
  • Glen,

    Thank you for the information, do you know of any tutorials or reference material, I could read on how to do what you recommended?


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet

    Friday, September 5, 2014 3:53 PM
  • To do HEAD request you can just use something simple like

                HttpWebRequest WRequest;
                HttpWebResponse WResponse;
                WRequest = (HttpWebRequest)HttpWebRequest.Create(service.Url);
                System.Net.CredentialCache credentialCache = new System.Net.CredentialCache();
                credentialCache.Add(service.Url, "Basic", ncCred);
                WRequest.Credentials = credentialCache;
                WRequest.PreAuthenticate = true;
                WRequest.KeepAlive = true;            
                WRequest.CookieContainer = new CookieContainer();
                WRequest.UserAgent = "Test Agent";
                WRequest.Method = "HEAD";
                WRequest.Headers.Add("Accept-Language:" + System.Globalization.CultureInfo.CurrentCulture.Name);
                try
                {
                    WResponse = (HttpWebResponse)WRequest.GetResponse();
                    WResponse.Close();
                }
                catch
                {
                   //process error 
                }

    With processing errors you should be doing it already depending on what method your using to access EWS eg If your using the ManagedAPI it will generated specific exceptions depending on what has gone wrong eg http://msdn.microsoft.com/en-us/library/office/microsoft.exchange.webservices.data.serviceresponseexception(v=exchg.80).aspx and also the ServiceRequestException http://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.servicerequestexception(v=exchg.80).aspx . So you should catch these exceptions in a normal TryCatch work out what happens and then develop your own retry/warning logic.

    Cheers
    Glen

    • Marked as answer by clayman2 Monday, September 15, 2014 1:07 PM
    Monday, September 8, 2014 7:37 AM
  • Sorry for the long delay, been a little busy. Glen thank you for the advice, I will take a look into this and figure out how to utilize it in my service.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet

    Monday, September 15, 2014 1:08 PM