简体   繁体   English

从 windows 服务调用 aspx 页面 - 问题

[英]Calling a aspx page from windows service - Problem

I have a windows service that calls a page after a certain interval of time.我有一个 windows 服务,它在一定时间间隔后调用一个页面。 The page in turn creates some reports.该页面反过来创建一些报告。 The problem is that the service stops doing anything after 2-3 calls.问题是该服务在 2-3 次调用后停止执行任何操作。 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因为它调用页面 2-3 次然后没有做任何工作,尽管它显示服务正在运行...我在我的服务中使用计时器..请有人在这里帮我解决问题 谢谢

the code:(where t1 is my timer)代码:(其中 t1 是我的计时器)

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. HttpWebRequest 可能会限制对特定页面或服务器的并发 HTTP 请求的数量,这通常是正确的 HTTP 客户端实践。

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).您没有正确处理对象的事实很可能意味着您正在维护到特定页面的 2 或 3 个连接,每个连接都有很大的超时值,并且 HttpWebRequest 正在排队或忽略您的请求,直到前几个完成(死于客户端或服务器超时,在这种情况下很可能是服务器)。

Add a 'finally' clause and dispose of your objects properly!添加“finally”子句并正确处理您的对象!

Well, what does the code look like?那么,代码是什么样的? WebClient is the easiest way to query a page: WebClient是查询页面的最简单方法:

    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.使用 WebClient 工作可靠,其中 WebRequest 超时。

@jscharf explanation looks as good as any to me. @jscharf 的解释对我来说看起来和任何解释一样好。

possibly the way you are requesting athe page is throwing an unnhandled exception which leaves the service in an inoperable state.您请求页面的方式可能会引发未处理的异常,从而使服务处于无法操作的 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.我认为您在处理 StreamReader、WebRequest 等对象时遗漏了一些东西。您应该在使用它们后处理昂贵的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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