Can't seem to make URL request from ElapsedEventHandler

Click For Summary
SUMMARY

The forum discussion centers on an issue with making URL requests from an ElapsedEventHandler in a Windows Service. The user confirmed that the method is being called every second but fails to execute a web request using .NET's WebClient, leading to no entries in the event log. The problem is likely related to permissions, with suggestions to run the service under the NetworkService account to resolve the issue.

PREREQUISITES
  • Understanding of C# asynchronous programming
  • Familiarity with Windows Services and ElapsedEventHandler
  • Knowledge of .NET's WebClient class
  • Basic concepts of Windows user account types and permissions
NEXT STEPS
  • Research how to configure Windows Services to run under different account types
  • Learn about handling asynchronous operations in C# with async/await
  • Explore alternative libraries for making web requests, such as HttpClient
  • Investigate common permissions issues in Windows Services
USEFUL FOR

Developers working with Windows Services, C# programmers dealing with asynchronous web requests, and anyone troubleshooting permissions issues in .NET applications.

SlurrerOfSpeech
Messages
141
Reaction score
11
I've spent 10+ hours on trying to solve this and can't get it to work!

In short, I have a method

Code:
        private async void LoadAndSavePage(object source, ElapsedEventArgs e)
        {
             // ... 
        }

which is being called every 1 second when the Windows Service that contains it is running. I've confirmed that it is indeed being called; I can verify with

Code:
        private async void LoadAndSavePage(object source, ElapsedEventArgs e)
        {
            EventLog.WriteEntry("I got called!");
        }

However, as soon as I try to get a web page, for example

Code:
        private async void LoadAndSavePage(object source, ElapsedEventArgs e)
        {
            string html = new WebClient().DownloadString("[PLAIN]http://physicsforums.com");[/PLAIN] 
            EventLog.WriteEntry(html);
        }

it doesn't work. The function gets called as scheduled but it never gets to the point where it writes to the event log. Something about introducing a web request messes it up. I'm guessing maybe some type of permissions issue?

I've tried with several libraries including .NET's WebClient and HtmlAgilityPack's HtmlWeb, I've tried with different URLs, etc.

I know it doesn't have to do with the event having a maximum resolution time, for I've also tried

Code:
        private async void LoadAndSavePage(object source, ElapsedEventArgs e)
        {
            EventLog.WriteEntry("before sleep");
            Thread.Sleep(1500);
            EventLog.WriteEntry("after sleep");
        }

and confirmed that the whole thing runs.
 
Last edited by a moderator:
Technology news on Phys.org
SlurrerOfSpeech said:
However, as soon as I try to get a web page, for example

Code:
        private async void LoadAndSavePage(object source, ElapsedEventArgs e)
        {
            string html = new WebClient().DownloadString("[PLAIN]http://physicsforums.com");[/PLAIN] 
            EventLog.WriteEntry(html);
        }

it doesn't work.

The function gets called as scheduled but it never gets to the point where it writes to the event log. Something about introducing a web request messes it up. I'm guessing maybe some type of permissions issue?
That would be my guess, but that's just a guess. In your following post, you suggest running the service as a NetworkService. I would give that a shot.
 
Last edited by a moderator:

Similar threads

  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 31 ·
2
Replies
31
Views
12K
  • · Replies 13 ·
Replies
13
Views
7K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 39 ·
2
Replies
39
Views
7K