WebRequest & WebResponse

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
I am trying to set up a monitoring service for a database. Originally I had a windows service running on the same machine as the database and querying the database every 10 minutes. It was possible to change the time automatically by changing a value that I was storing in the machine's registry.

I have been told that I need to relocate this service, as if the machine goes down, the service will not be available. I have been told also that opening the sql server (2005) to allow remote connections is not an option. The solution that I am hoping to use is to install a web page (.aspx) on the same machine as the database and use my windows service installed on another machine to send web requests to the web page, and get a specific web response.

This is the code that I would be using to sending the request and retrieving the response:
Code:
 WebRequest wRequest;
        WebResponse  wResponse;
        System.IO.StreamReader sReader;

        wRequest = System.Net.HttpWebRequest.Create("http://127.0.0.1/WebMonitor/Monitor.aspx");
        wRequest.Method = "GET";
        wResponse = wRequest.GetResponse();

        sReader = new System.IO.StreamReader(wResponse.GetResponseStream());
        String result;
        result = sReader.ReadToEnd().Trim();
        sReader.Close();

The problem that I am having is that I am unable to find any information on providing my own specific response from the web page. The value that I need to send back is a float. Any suggestions on how I can do this?

Mike55.
 
Web service

I think a web service would be much more suitable than a web page, as it can expose methods which take strongly typed parameters and have strongly typed return values. Web service methods are also easier to invoke that plain web pages.

Your web service code would look something like this:

Code:
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

public class MyWebService : WebService
{
    [WebMethod()]
    public float MonitorDatabase()
    {
        //Do action

        return someValue;
    }
}

In the windows service project, you can then add a web reference to this web service, and use it like any other class:

Code:
MyWebService serv = new MyWebService();

retVal = serv.MonitorDatabase();

Good luck :)
 
Re: Web service

Have previously used web services with windows services, however I ran in the problems, in that the windows service seems to have lost its reference to the web service. Don't particularly want to get caught out again a second time. Is there a simple way of supplying a web references, without right clicking on references and going "add web reference"?

Mike55.
 
I believe that when you add the web ref that way in studio it actually dumps something in the web.config file with the url. But i don't know if something gets compiled as well which would mean that you could not redirect the url after you have deployed. I have also experienced times when the web ref seems to have been 'lost'. Also don't forget that you need to refresh it if you change the web method code. I think when you set the ref through studio alot of things happen so i would stick with that way of doing it. You want that proxy to be created for you, it gives you intellinonsense when working with the web service.
 
Back
Top