简体   繁体   中英

Calling a aspx page from windows service - Problem

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)
        {
        }
    }

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!

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's advice worked for me, in the context of a service

Using WebClient worked reliably, where WebRequest timed out.

@jscharf explanation looks as good as any to me.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM