views:

348

answers:

4

I have a windows service that calls a page after a certain interval of time. The page in turn creates some reports. The problem is that the service stops doing anything after 2-3 calls. as in it calls the page for 2-3 times and then does not do any work though it shows that the service is running...i am using timers in my service.. please can someone help me with a solution here thank you

the code:(where t1 is my timer)

protected override void OnStart(string[] args)
    {
            GetRecords();
            t1.Elapsed += new ElapsedEventHandler(OnElapsedTime);

            t1.Interval = //SomeTimeInterval
            t1.Enabled = true;
            t1.Start();

    }

    private void OnElapsedTime(object source, ElapsedEventArgs e)
    {
        try
        {
            GetRecords();
        }
        catch (Exception ex)
        {
            EventLog.WriteEntry(ex.Message);
        }

    }

    public void GetRecords()
    {


        try
        {
            string ConnectionString = //Connection string from web.config
            WebRequest Request = HttpWebRequest.Create(ConnectionString);
            Request.Timeout = 100000000;
            HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();


        }
        catch (Exception ex)
        {
        }
    }
A: 

Well, what does the code look like? WebClient is the easiest way to query a page:

    string result;
    using (WebClient client = new WebClient()) {
        result = client.DownloadString(address);
    }
    // do something with `result`

The timer code might also be glitchy if it is stalling...

Marc Gravell
WebRequest Request = HttpWebRequest.Create(ConnectionString); Request.Timeout = 100000000; HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();thats how i am calling thepage
programmer
A: 

possibly the way you are requesting athe page is throwing an unnhandled exception which leaves the service in an inoperable state.

Yes, we need code.

Jason
try/catch block at timer - OnElaspedTime should catch the exceptions....(Thats what i am doing at the moment)
programmer
i just posted my code...can u please tell me if m missing anything
programmer
A: 

I think you're missing something about disposing your objects like StreamReader, WebRequest, etc.. You should dispose your expensive objects after using them.

Canavar
i timeout the webrequest so i dont think thats the problem....
programmer
I meaned Stream returned by this method : response.GetResponseStream()You should write this stream and the other objects if you use StreamReader after that code.
Canavar
Or use `WebClient` and let *it* worry about it...
Marc Gravell
k i'll try streamreader and check...thanks
programmer
i just implemented streamreader in my service...n will Response.close() be of any help???
programmer
hey!!!my service is working fine...thanks2u!!!!
programmer
but how did you solve the problem ? Is my answer helped you ??
Canavar
i used streamreader to read the response till the end...used some try/catch blocksn i also kept the timer interval to 30mins as opposed to before which was 1-2 minutes.n it worked..so i thnk the reader solved the problem mostly...wht do u thnk?m still not sure how it worked but m not complaining
programmer
It's interesting. As Marc and I said we thought your not disposed resources produce this error.
Canavar
A: 

It's possible that HttpWebRequest will restrict the number of concurrent HTTP requests to a specific page or server, as is generally proper HTTP client practice.

The fact that you're not properly disposing your objects most likely means you are maintaining 2 or 3 connections to a specific page, each with large timout value, and HttpWebRequest is queueing or ignoring your requests until the first few complete (die from a client or server timeout, most likely the server in this case).

Add a 'finally' clause and dispose of your objects properly!

jscharf