Contents tagged with C#

  • Call a Function With Retry Using Generics


    What if you like to call a web service and retry the call again until response? let me show you a nice way to call a function with retry until you retrieve what ever is needed from the function. This could be achieved by using generics especially by extending the Func<T> delegate; check the code below;

       1: public static class MyExtensions
       2:     {
       3:         public static T WithRetry<T>(this Func<T> action)
       4:         {
       5:             var result = default(T);
       6:             int retryCount = 0;
       7:             var succesful = false;
       8:             do
       9:             {
      10:                 try
      11:                 {
      12:                     result = action();
      13:                     succesful = true;
      14:                 }
      15:                 catch (Exception ex)
      16:                 {
      17:                     retryCount++;
      18:                 }
      19:             } while (retryCount < 3 && !succesful);
      20:             return result;
      21:         }
      22:     }

    In the code you can see that we extended the Func that will return an object with type T. This function will keep trying for 3 times but you might change it to keep it trying until it achieve the goal. and here is how to use it


       1: MyService ser = new MyService();
       2: Func<MyService.contentSetList> fCon = () => ser.get(testlist.ToArray<string>());
       3: var con = fCon.WithRetry();
       4: return con;


    hope this helps.

    References: the original code belongs to Scott Allen at his course C# Fundamentals - Part 2 @ http://www.pluralsight-training.net. thanks Scott.

  • C# 4.0 IN NUTSHELL

    I found this book very useful and a must-read. The book is really a good reference for C# in general and C# 4.0. Thanks Albahari

  • Using 2 Tables Joined with LINQ as data source without anonymous cast error in databond method

    Yesterday one of our project team member faced a challenge of using an anonymous data that is returned from joining 2 typed data tables with LINQ. The problem is not how to use the data, the problem was how to be able to cast and use the data in Repeater ItemDataBond method without having “<>f__AnonymousType0….” cast error. below is the join query (tables used are typed) :-

  • Log Off OWA from Code Behind

     After many tries and havey googling to find out the best way to log off OWA from asp.net page code behind while logging out from my main application, i found out that this can be achieved in one way ( in my position). Rise a small popup window and call OWA log out page!

  • Access OWA with C# inside Asp.net Site

    public partial class OpenOwaPage : System.Web.UI.Page
    {
        private string userName;
        private string passWord;

        protected void Page_Load(object sender, EventArgs e)
        {
          
                passWord = "password";
                userName ="username";
                Response.Write(CreateOWAFrom());
                Response.Write(LoadOWAPostJS("logonForm"));
          
        }

        private string LoadOWAPostJS(string strFormId)
        {
            //Constructs the JS needed to post the data to Realex and returns it
            StringBuilder strScript = new StringBuilder();
            strScript.Append("<script language='javascript'>");
            strScript.Append("var ctlForm = document.forms.namedItem('{0}');");
            strScript.Append("ctlForm.username.value=\"" + userName + "\";");
            strScript.Append("ctlForm.password.value=\"" + passWord + "\";");
            strScript.Append("ctlForm.submit();");
            strScript.Append("</script>");
            return String.Format(strScript.ToString(), strFormId);

        }

        private string CreateOWAFrom()
        {
            //Constructs the Realex HTML form and returns it
            StringBuilder strForm = new StringBuilder();
            strForm.AppendLine("<form id=\"logonForm\" name=\"logonForm\" target=\"_self\" action=\"https://your_Owa_Adress/exchweb/bin/auth/owaauth.dll\" method=\"post\">");
            strForm.AppendLine("<input type=\"hidden\" name=\"destination\" value=\"https://your_Owa_Adress/exchange/\"/>");
            strForm.AppendLine("<input type=\"hidden\" name=\"flags\" value=\"0\"/>");
            strForm.AppendLine("<input type=\"hidden\" name=\"username\" id=\"username\"/>");
            strForm.AppendLine("<input type=\"hidden\" name=\"password\" id=\"password\"/>");
            strForm.AppendLine("<input type=\"hidden\" id=\"SubmitCreds\" name=\"SubmitCreds\" value=\"Connection\"/>");
            strForm.AppendLine("<input type=\"hidden\" id=\"rdoRich\" name=\"forcedownlevel\" value=\"0\"/>");
            strForm.AppendLine("<input type=\"hidden\" id=\"rdoPublic\" name=\"trusted\" value=\"0\"/>");
            strForm.AppendLine("</form>");
            return strForm.ToString();

        }

    Sometimes you maybe not able to access your OWA because of your Browser configuration, add your OWA website address to your Browser trusted site to solve this problem. If you do not want to logoff from OWA with the regular logoff button, read this (click here). Otherwise you'll enter OWA any time you type OWA address.