Does anyone know if you can use HttpWebRequest in a Windows Service?

I have a small app that checks an IP, works perfectly as a Forms App but does nothing when I convert it too a service.

Heres my code:

Imports System.Net
Imports System.IO

Public Class Service1

    'declare timer
    Private TimerCheckIP As New System.Timers.Timer(30000)


    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.

        'initiat Timers
        AddHandler TimerCheckIP.Elapsed, AddressOf TimerCheckIP_Elapsed
        TimerCheckIP.Enabled = True
        TimerCheckIP.Start()

    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
    End Sub

    'Timers
    Private Sub TimerCheckIP_Elapsed(ByVal pSender As Object, ByVal pArgs As System.Timers.ElapsedEventArgs)
        'get External IP 
        Try
            Dim req As HttpWebRequest = WebRequest.Create("http://whatismyip.com/automation/n09230945.asp")
            Dim res As HttpWebResponse = req.GetResponse()
            Dim Stream As Stream = res.GetResponseStream()
            Dim sr As StreamReader = New StreamReader(Stream)

            Dim currentIP As String
            currentIP = (sr.ReadToEnd())
            'MessageBox.Show(currentIP)

            Dim sSource As String
            Dim sLog As String
            Dim sEvent As String
            Dim sMachine As String

            sSource = "Tutorial Service"
            sLog = "IP Status"
            sEvent = "External IP: " + currentIP
            sMachine = "."
            ' Check if the the Event Log Exists 
            If Not EventLog.SourceExists(sSource, sMachine) Then
                EventLog.CreateEventSource(sSource, sLog, sMachine) ' Create Log 
            End If

            Dim ELog As New EventLog(sLog, sMachine, sSource)
            ELog.WriteEntry(sEvent, EventLogEntryType.Information, 100, CType(1, Short))

        Catch ex As Exception
            'MessageBox.Show(ex.Message)

            Dim sSource As String
            Dim sLog As String
            Dim sEvent As String
            Dim sMachine As String

            sSource = "Tutorial Service"
            sLog = "IP Status"
            sEvent = "Unable to Get External IP: " + ex.Message
            sMachine = "."
            ' Check if the the Event Log Exists 
            If Not EventLog.SourceExists(sSource, sMachine) Then
                EventLog.CreateEventSource(sSource, sLog, sMachine) ' Create Log 
            End If

            Dim ELog As New EventLog(sLog, sMachine, sSource)
            ELog.WriteEntry(sEvent, EventLogEntryType.Information, 100, CType(1, Short))

        End Try

    End Sub

What are you running your service as? Some of the service accounts dont have access to networking componenets.

For testing try to switch your service over to run as administrator and see if it starts working.

This article has been dead for over six months. Start a new discussion instead.