Monday, March 12, 2012

How to test your configuration on servers to eliminate Issues before opening to production.










  
Code Snippet
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="NewPage.aspx.cs" Inherits="TestConnection_NewPage" %>
  2. DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html >
  4. <head id="Head1" runat="server">    
  5. head>
  6. <title>jQuery UI Tabs - Open on mouseovertitle>    
  7. <link type="text/css" href="jquery-ui-1.8.custom.css" rel="Stylesheet"/>
  8. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js">script>    
  9. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.js">script>
  10.     
  11.     
  12.     <style type="text/css">.style3 { FONT-SIZE: x-large; COLOR: #ffffff }
  13.         .style4 { FONT-FAMILY: Verdana; TEXT-ALIGN: center }
  14.         .style5 { FONT-SIZE: medium }
  15.         .style6 { FONT-WEIGHT: bold; FONT-SIZE: medium; FILTER: dropshadow(color=#000000,offx=2,offy=2); COLOR: #0b77d3; FONT-FAMILY: Verdana; TEXT-ALIGN: center }
  16.         .moduleGroupDivStyle {width:100%; }
  17.         .appGroupHeadingStyle {width:100%; height:20px; padding-left:20px; text-align:left; vertical-align:bottom; font-weight:bold; font-size:12px; background-color:LightGrey;}
  18.         .RadioButtonStyle {font-size:11px; }
  19.     style>
  20.  
  21. <script type="text/javascript">
  22.     $(function () {
  23.         $("#tabs").tabs({
  24.             event: 'mouseover'
  25.         });
  26.     });
  27.     script>
  28.  
  29. <body>
  30.  
  31. <div class="demo">
  32. <div id="displayLoading"><span style="background-color:Yellow;font-size:larger;">Loading.... span>div>
  33. <table cellspacing="0" cellpadding="0" width="100%" border="0">
  34.                 <tbody>
  35.                     <tr>
  36.                            <td valign="bottom" nowrap width="29%" background="di1.gif" height="55">
  37.                              td>
  38.                         <td class="style3" nowrap background="di1.gif" height="55">
  39.                             Validate Web.config td>
  40.                         <td valign="bottom" nowrap width="22%" background="di1.gif">
  41.                              td>
  42.                     tr>
  43.                 tbody>
  44. table>
  45. <table width="70%">
  46. <tr>
  47.     <td width="10%"> td><td style="height:1cm"> td>
  48. tr>
  49.  
  50. <tr>
  51.     <td width="10%"> td><td><center><h5>Webservices, nHibernate Mapping Files, Database Connectionsh5>center>td>
  52. tr>
  53. <tr>
  54.     <td> td><td> td>
  55. tr>
  56. <tr>
  57. <td>
  58.  
  59. td>
  60. <td>
  61. <div id="tabs">
  62.     <ul>
  63.         <li><a href="#tabs-1">Web Service URLsa>li>
  64.         <li><a href="#tabs-2">Nhibernate Mappinga>li>
  65.         <li><a href="#tabs-3">Connection Stringa>li>
  66.     ul>
  67.     <div id="tabs-1">
  68.         <p>
  69.         
  70.         <asp:Table ID="wsListTable" runat="server" BorderWidth="1" BackColor="AliceBlue"
  71.             Font-Size="Larger" CellPadding="2" CellSpacing="2" BorderColor="Black">
  72.             <asp:TableRow BorderWidth=1 Font-Bold="true">
  73.                 <asp:TableCell BorderWidth="1" Width="10%" VerticalAlign="Middle" align="center">Serial Noasp:TableCell>
  74.                 <asp:TableCell BorderWidth="1" Width="40%">URL/Connectionasp:TableCell>
  75.                 <asp:TableCell BorderWidth="1" Width="10%" align="center">Statusasp:TableCell>
  76.             asp:TableRow>
  77.         asp:Table>   
  78.         <br/>
  79.         <br/>
  80.         <button id="TestConn">Test Webservice button><span style="background-color:Yellow;font-size:larger;" id="lblstatus">span>
  81.     p>
  82.     div>
  83.     <div id="tabs-2">
  84.         <p>
  85.             <span id="TestConnection">span>
  86.              <br/>
  87.             <br/>
  88.             <button id="TestDatabase">Test Mappingbutton>
  89.         p>
  90.     div>
  91.     <div id="tabs-3">
  92.         <p><asp:Table ID="wsDatabaseConnection" runat="server" BorderWidth="1" BackColor="AliceBlue"
  93.             Font-Size="Larger" CellPadding="2" CellSpacing="2" BorderColor="Black">
  94.         <asp:TableRow BorderWidth=1 Font-Bold="true">
  95.             <asp:TableCell BorderWidth="1" Width="10%" VerticalAlign="Middle" align="center">Nameasp:TableCell>
  96.             <asp:TableCell BorderWidth="1" Width="30%">Connection stringasp:TableCell>
  97.             <asp:TableCell BorderWidth="1" Width="10%" align="center">Providerasp:TableCell>
  98.         asp:TableRow>
  99.     asp:Table> p>
  100.     div>
  101. div>
  102. td>
  103. tr>
  104. table>
  105. div>
  106.  
  107. <script type="text/javascript">
  108.  
  109.      $(document).ready(function () {
  110.          $("#displayLoading").hide();
  111.      });
  112.      $("#TestDatabase").click(function () {
  113.  
  114.          var webMethod = 'newpage.aspx/TestAllMapping'
  115.          var resultId = "#ResultData";
  116.  
  117.  
  118.          $.ajax({
  119.              type: "POST",
  120.              url: webMethod,
  121.              data: "{'name': '" + $(this).attr("id") + "'}",
  122.              contentType: "application/json; charset=utf-8",
  123.              dataType: "json",
  124.              success: function (msg) {
  125.                  $("#TestConnection").html(msg.d);
  126.              },
  127.              error: AjaxFailed
  128.          });
  129.      });
  130.  
  131.      $("#TestConn").click(function () {
  132.  
  133.          resetResults();
  134.          testConnections();
  135.  
  136.      });
  137.  
  138.      function resetResults() {
  139.  
  140.          $("td").each(function () {
  141.              var resultId = "#" + $(this).attr("id") + "Result";
  142.              $(resultId).text("");
  143.          });
  144.      }
  145.  
  146.      function testConnections() {
  147.          $("td").each(function () {
  148.              if ($(this).text().substring(0, 4) == "http") {
  149.  
  150.                  var webMethod = 'newpage.aspx/TestHTTP'
  151.                  var resultId = "#" + $(this).attr("id") + "Result";
  152.                  $.ajax({
  153.                      type: "POST",
  154.                      url: webMethod,
  155.                      timeout: 3000,
  156.                      data: "{'name': '" + $(this).attr("id") + "'}",
  157.                      contentType: "application/json; charset=utf-8",
  158.                      dataType: "json",
  159.                      success: function (msg) {
  160.                          if (msg.d == "False") {
  161.  
  162.                          }
  163.                          $(resultId).html(msg.d);
  164.  
  165.                      },
  166.                      async: false,
  167.                      error: AjaxFailed
  168.                  });
  169.              }
  170.          });
  171.      }
  172.  
  173.      function AjaxFailed(result) {
  174.          alert(result.status + ' ' + result.statusText);
  175.      }
  176.  
  177.     script>
  178. body>
  179. html>


Code Snippet
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Data.OracleClient;
  6. using System.Data.SqlClient;
  7. using System.Net;
  8. using System.Text;
  9. using System.Web.UI.WebControls;
  10.  
  11.  
  12. public partial class TestConnection_NewPage : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.  
  17.         int nCounter = 0;
  18.  
  19.  
  20.         foreach (string appKey in ConfigurationManager.AppSettings.AllKeys)
  21.         {
  22.             string appKeyValue = ConfigurationManager.AppSettings.Get(appKey);
  23.             if (appKeyValue.Contains("http") && !appKeyValue.Contains("swe"))
  24.             {
  25.                 TableRow tRow = new TableRow();
  26.                 tRow.BorderWidth = 1;
  27.                 // Create a new cell and add it to the row.
  28.                 TableCell tCell1 = new TableCell();
  29.                 tCell1.Text = (++nCounter).ToString();
  30.                 tCell1.BorderWidth = 1;
  31.                 tRow.Cells.Add(tCell1);
  32.  
  33.                 TableCell tCell2 = new TableCell();
  34.                 tCell2.Text = appKeyValue;
  35.                 tCell2.BorderWidth = 1;
  36.                 tCell2.ID = appKey;
  37.                 tRow.Cells.Add(tCell2);
  38.  
  39.  
  40.                 TableCell tCell3 = new TableCell();
  41.                 tCell3.BorderWidth = 1;
  42.                 tCell3.ID = appKey.Trim() + "Result";
  43.                 tRow.Cells.Add(tCell3);
  44.                 wsListTable.Rows.Add(tRow);
  45.  
  46.             }
  47.         }
  48.  
  49.         foreach (ConnectionStringSettings connection in ConfigurationManager.ConnectionStrings)
  50.         {
  51.             string name = connection.Name;
  52.             string provider = connection.ProviderName;
  53.             string connectionString = connection.ConnectionString;
  54.             string statusCheck = string.Empty;
  55.  
  56.             TableRow tRow = new TableRow();
  57.             tRow.BorderWidth = 1;
  58.             // Create a new cell and add it to the row.
  59.             TableCell tCell1 = new TableCell();
  60.             tCell1.Text = name;
  61.             tCell1.BorderWidth = 1;
  62.             tRow.Cells.Add(tCell1);
  63.  
  64.             TableCell tCell2 = new TableCell();
  65.             tCell2.Text = connectionString;
  66.             tCell2.BorderWidth = 1;
  67.             tCell2.ID = name;
  68.             tRow.Cells.Add(tCell2);
  69.  
  70.             try
  71.             {
  72.                 if (provider == "System.Data.SqlClient")
  73.                 {
  74.                     SqlConnection conn = new SqlConnection(connectionString);
  75.  
  76.                     conn.Open();
  77.                     if (conn.State == ConnectionState.Open)
  78.                         statusCheck = "Success";
  79.                 }
  80.                 if (provider == "System.Data.OracleClient")
  81.                 {
  82.                     OracleConnection conn = new OracleConnection(connectionString);
  83.                     conn.Open();
  84.                     if (conn.State == ConnectionState.Open)
  85.                         statusCheck = "Success";
  86.  
  87.                 }
  88.             }
  89.             catch (Exception exp)
  90.             {
  91.                 statusCheck = exp.Message.ToString();
  92.             }
  93.  
  94.             TableCell tCell3 = new TableCell();
  95.             tCell3.BorderWidth = 1;
  96.             tCell3.ID = name.Trim() + "Result";
  97.             tCell3.Text = statusCheck;
  98.             tRow.Cells.Add(tCell3);
  99.             wsDatabaseConnection.Rows.Add(tRow);
  100.  
  101.         }
  102.     }
  103.     [System.Web.Services.WebMethod]
  104.     public static string TestHTTP(string name)
  105.     {
  106.         //= "url";
  107.         string appKey = name;
  108.         string appKeyValue = ConfigurationManager.AppSettings.Get(appKey);
  109.  
  110.         if (appKeyValue.Contains("http"))
  111.         {
  112.             if (!(appKeyValue.Contains("wsdl") || appKeyValue.Contains("WSDL")))
  113.             {
  114.                 appKeyValue += "?wsdl";
  115.             }
  116.         }
  117.  
  118.         return ConnectionAvailable(appKeyValue) ? "<span style='color:Green'>Passedspan>" : "<span style='color:Red'>Failedspan>";
  119.  
  120.     }
  121.     public static bool ConnectionAvailable(string strServer)
  122.     {
  123.         try
  124.         {
  125.             HttpWebRequest reqFP = (HttpWebRequest)HttpWebRequest.Create(strServer);
  126.  
  127.             HttpWebResponse rspFP = (HttpWebResponse)reqFP.GetResponse();
  128.             if (HttpStatusCode.OK == rspFP.StatusCode)
  129.             {
  130.                 // HTTP = 200 - Internet connection available, server online
  131.                 rspFP.Close();
  132.                 return true;
  133.             }
  134.             else
  135.             {
  136.                 // Other status - Server or connection not available
  137.                 rspFP.Close();
  138.                 return false;
  139.             }
  140.         }
  141.         catch (WebException)
  142.         {
  143.             // Exception - connection not available
  144.             return false;
  145.         }
  146.     }
  147.  
  148.     [System.Web.Services.WebMethod]
  149.     public static string TestAllMapping()
  150.     {
  151.         StringBuilder stb = new StringBuilder();
  152.  
  153.         stb.AppendFormat("<tr style='height:10'><td><b>{0}b>td><td><b>{1}b>td><td><b>{2}b>td>tr>", "Table Name", "Exception", "Nhibernate Object");
  154.         using (OES.Library.nHibernate.Repository rep = new OES.Library.nHibernate.Repository())
  155.         {
  156.  
  157.             IDictionary allClassMetadata = rep.Session.SessionFactory.GetAllClassMetadata();
  158.             foreach (DictionaryEntry entry in allClassMetadata)
  159.             {
  160.                 try
  161.                 {
  162.  
  163.                     NHibernate.Persister.Entity.SingleTableEntityPersister tableEntity = entry.Value as NHibernate.Persister.Entity.SingleTableEntityPersister;
  164.                     if (tableEntity != null)
  165.                     {
  166.                         int index = tableEntity.ClassMetadata.EntityName.Split('.').Length;
  167.                         if (tableEntity.TableName.Split('.')[1] != tableEntity.ClassMetadata.EntityName.Split('.')[index - 1])
  168.                         {
  169.  
  170.                             rep.Session.CreateCriteria((Type)entry.Key)
  171.                                  .SetMaxResults(0).List();
  172.                         }
  173.                     }
  174.                 }
  175.                 catch (Exception exp)
  176.                 {
  177.  
  178.                     NHibernate.Persister.Entity.SingleTableEntityPersister tableEntity = entry.Value as NHibernate.Persister.Entity.SingleTableEntityPersister;
  179.  
  180.                     stb.AppendFormat("<tr><td>{0}td><td>{1}td><td>{2}td>tr>", tableEntity.TableName, exp.InnerException.Message, entry.Key);
  181.  
  182.  
  183.                 }
  184.             }
  185.             if (stb.Length == 0)
  186.             {
  187.                 stb.Append("Sucess");
  188.             }
  189.         }
  190.         return GenerateTable(stb).ToString();
  191.     }
  192.     private static StringBuilder GenerateTable(StringBuilder sbTableContent)
  193.     {
  194.         StringBuilder sbTable = new StringBuilder();
  195.         sbTable.AppendFormat("<Table cellspacing='0' border='1'>{0}Table>", sbTableContent.ToString());
  196.  
  197.         return sbTable;
  198.     }
  199. }