1

I have some code that consumes a webservice. Rather than retrieve a session key from the service each time, I want to try the response with a session variable I set, and if I get a 403 error get a new session key and try again.

In my limited mind nesting try/catch statements makes sense but I'm just good enough to realize there has to be a better way. I've read a few posts here mentioning things like lambda expressions with using statements and Helpers, but these may as well be gifts from an alien interplanetary science to me.

Always willing to learn I was wondering if anyone has suggestions (with details or links to details)? Everything here works, I just need a better way than nesting try statements (please and thank you). As always, you guys rock!

public string getMySessionkey goes to the server with login credentials to get a session key (required for all other inquiries) if there isn't a session key already set

searchPacket = stringbuilder with parameters and session key

if (Session["MySessionKey"] != null)
{
    sessionKee = Session["MySessionKey"].ToString();
}
else
{
    sessionKee = getMySessionkey(_cbE, _cbP);
    System.Web.HttpContext.Current.Session.Add("MySessionKey", sessionKee);
}

try
{
    mySearchResults = getResults(searchPacket.ToString());
    vbResultz += Server.HtmlEncode(mySearchResults) ;
}
catch (WebException wx)
{
    HttpWebResponse webresponse ;
    webresponse = (HttpWebResponse)wx.Response;

    switch (webresponse.StatusCode)
    {
       case HttpStatusCode.InternalServerError:
            ...
            break;

        case HttpStatusCode.Forbidden: // 403
            vbResultz = "You aint got no valid session key!";
          // code here to get a new session key and try again

            break;

        default:
            throw;
    }
}

1 Answer 1

2

Use a loop with a limited occurences number. As soon as your call works, exit the loop.

var maxNumberOfTries = 3;
var currentTry = 0;
var success = false;

do
{
    currentTry += 1;

    // Try whatever here. If it works, set success variable to true

    if (success) break;
} while (currentTry <= maxNumberOfTries)

if (!success)
{
    // If code reaches here, whatever had to be done has been tried <maxNumberOfTries> times and did not work
}

EDIT: Here is the above sample implemented in your code:

var maxNumberOfTries = 3;
var currentTry = 0;
var flag = false;

do
{
    currentTry += 1;

    try
    {
        mySearchResults = getResults(searchPacket.ToString());
        vbResultz += Server.HtmlEncode(mySearchResults) ;
        flag = true;
    }
    catch (WebException wx)
    {
        HttpWebResponse webresponse ;
        webresponse = (HttpWebResponse)wx.Response;

        switch (webresponse.StatusCode)
        {
           case HttpStatusCode.InternalServerError:
            ...
                flag = true;
                break;

            case HttpStatusCode.Forbidden: // 403
                vbResultz = "You aint got no valid session key!";
              // code here to get a new session key and try again

                break;

            default:
                throw;
        }
    }
    if (flag) break;
} while (currentTry <= maxNumberOfTries)

if (!flag)
{
    // If code reaches here, whatever had to be done has been tried <maxNumberOfTries> times and did not work
}

Here the flag variable will be set to true on successful attempt to obtain a key, OR if an internal server error occurs. If you have 403 response it will try again, up to three times.

Sign up to request clarification or add additional context in comments.

2 Comments

works like a charm, thanx so much. Still want to learn about lamba and helpers, though.
@user116923 I'm not quite sure how that would benefit you on that specific problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.