views:

242

answers:

4

Hi

I need to write a simple WinForms apps that can be fired to test if a website is still alive and that that website is able to read from a database.

I am using the whole "(HttpWebResponse)myHttpWebRequest.GetResponse()" thing in c# to test whether the site is alive, but I am at a lose for how to get a test page in my website to write something to the "Response" to indicate that it was able to test it's own connectivity to the database.

Here is the sample code for my Winforms side (ripped from the MSDN):

private void CheckUrl()
{
  try
  {
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com");

    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
    myHttpWebResponse.Close();

    label1.Text = myHttpWebRequest.Address.AbsoluteUri;
  }
  catch (WebException e)
  {
    label1.Text = "This program is expected to throw WebException on successful run." +
                                    "\n\nException Message :" + e.Message;

    if (e.Status == WebExceptionStatus.ProtocolError)
    {
      label1.Text = String.Format("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
      label2.Text =String.Format("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
    }
  }
  catch (Exception e)
  {
    label1.Text = e.Message;
  }
}

I was hoping for some help on the webform side of things to return to the above code.

Thanks for any help that you folks can provide.

  • Richard
+2  A: 

You can create a webservice inside of the project called IAMALIVE and have it return a single char.

On your WinForms area, consume said WebService and if it works, your site is alive.

Sergio Tapia
Hi. We thought of this, but it doesn't seem like the server is freaking out, it's a specific web app. We just need to keep an eye on the site throughout the day while we are trying to fix the specific problem.Thanks for responding though.
Richard O'Neil
+1 for inspiring my answer :)
JoshJordan
+2  A: 

In the essence of Papuccino's answer: you can actually create web services that are embedded in the C# code-behind of your WebForms pages by marking them with the [WebMethod] attribute. Those will reside within the web application, not just the server.

JoshJordan
Hi JoshI actually did not know that you could do that. The only web service work that I have done has been the creation of an entire "web service". I then created a WSDL file and can hit it using whatever I want.I'm a bit unsure of how to move forward with your suggestion. Are you saying that I can take an existing page in the app and add a [WebMethod] to it and then generate a WSDL file somehow and then access it?If so, that may very well be the perfect way for me to go.I really appreciate all of you guys taking time to help me with this.
Richard O'Neil
It's even simpler! Just create another project within your solution of WebService type.The IDE generates most of what you need on the spot. Just follow the syntax of the HelloWorld webmethod and create a method that returns a single char or byte, whatever. :D
Sergio Tapia
+1  A: 

What happens when your site fails? Does it return a 500 status code or timeout?

Another way to look at it: does it always do something expected if it succeeds?

You might call a URL in your web app that you know will either return a 200 response code or will have some expected HTML markup in the response if things are working fine.

Have your winform call this URL and examine the Response.status or the text in the output buffer for your expected markup. You should also create a timeout in your httprequest. If the page does not load within the timeout, you will get a web exception and you will know the site is failing.

Also, if you have the budget, there are external monitoring services like gomez.com that can automate this and provide reporting on site availability.

Matt Wrock
Hey MattI think it's returning a 500 status code right now. I don't have it right here in front of me. I'm just watching Cities of the Underworld right now, and it popped into my head that somebody here might be able to point me in the right direction.<br>I think I can make a page that would look up a key value in a database and return this value to me, so that I would always know what to look for. I was just trying to discover how to get that in my WinForms apps. That was the part that I wasn't sure how to do.<br>Thanks.
Richard O'Neil
Hey Everybody...I'm so sorry that I didn't come back to set the accepted answer. Everybody was so useful and helpful while I needed the help, it was hard to click on any particular one, but I ended up implementing something like Matt's suggestion and it's been working perfectly.
Richard O'Neil
+1  A: 

have your webform page open a database connection and perform something simple/low-impact, e.g.

select SystemTableId from dbo.[SystemTable] where SystemTableId = 1

where SystemTable is a single-row table.

If the page throws an exception for any reason, Response.Write the exception message, otherwise Response.Write("SUCCESS") or similar.

Steven A. Lowe