0

So I have this Windows service created in VB.Net, and all it does is perform a few calls and sits there. (Does nothing really)

I have it installed on a test machine and when I start it manually, it starts up. When I stop manually, shuts down properly (onStop is called).

When I start it, and reboot the machine, the service starts like it should, but when I log in, the service isn't running anymore and the onStop never showed up as being executed.

I installed the service with admin privileges, so that shouldn't be an issue.

Here is what I have:

Sub New()

    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    '' Open our log file
    FileReader = New IO.StreamWriter("C:\test.txt", True)



    Log("Starting service...")

    '' Get the username from the process list (If explorer is open (Meaning a user is logged in)
    Dim ProcessList As System.Diagnostics.Process()
    ProcessList = System.Diagnostics.Process.GetProcesses()
    Dim Proc As System.Diagnostics.Process
    For Each Proc In ProcessList
        If Proc.ProcessName.ToLower() = "explorer" Then
            My.Settings.Username = GetProcessUserName(Proc)
            Exit For
        End If
    Next

    Log("Fetched user name: " & My.Settings.Username)
    Try
        '' Get the IP address of the machine
        Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
        My.Settings.IP = h.AddressList.GetValue(0).ToString

        '' Get the Machine Information
        My.Settings.MachineName = System.Environment.MachineName()

        '' Get the Operating System Version
        Dim osInfo As New FindOsInfo
        My.Settings.OS = osInfo.OsName.ToString().Replace(" ", "").Split("|")(0)

    Catch ex As Exception
        Log("Startup error: " & ex.Message)
    End Try


    '' Start server listen here


End Sub

'Protected Overrides Sub OnStart(ByVal args() As String)

'End Sub

Protected Overrides Sub OnStop()
    ' Add code here to perform any tear-down necessary to stop your service.
    Log("Closing down service...")
    FileReader.Close()

End Sub

    Public Sub Log(ByVal str As String)
        Dim time As DateTime = DateTime.Now
        FileReader.WriteLine("[" & time.ToString("G") & "] " & str)
        FileReader.Flush()
    End Sub

Thanks for your time!

dab
  • 817
  • 1
  • 12
  • 23

2 Answers2

2

An exception is probably thrown during automatic startup, causing the process to abort (and the service to stop).

You need to add logging to the service in order to determine the cause of the exception - resolve the cause and you are good to go.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • I have a file based logging, however I have a feeling that's not the type of logging you're referring to. Would you happen to have a link to a place demonstrating the "proper" logging methodologies? – dab Nov 07 '11 at 17:04
  • @dab - Nothing wrong with file based logging. I was thinking about logging to the event log, however. http://stackoverflow.com/questions/1133355/c-sharp-writing-to-the-event-viewer – Oded Nov 07 '11 at 17:09
  • So it was an error causing it, but there wasn't an exception being thrown (poorly coded class that I was using). Thanks for the answer! – dab Nov 08 '11 at 18:10
1

The issue is most likely that My.Settings.UserName is nothing when you reboot the machine since there isn't an interactive user, so your statement:

 Log("Fetched user name: " & My.Settings.Username)

is throwing an exception. You should move all of your code after:

Log("Starting service...")

inside the Try and also add a test for the existence of username before logging it:

If My.Settings IsNot Nothing AndAlso My.Settings.UserName IsNot Nothing Then
     Log("Fetched user name: " & My.Settings.Username)
Else
     Log("No interactive user")
End if
competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • Aha, I didn't think about that. I'll give that a try. – dab Nov 07 '11 at 18:23
  • That didn't seem to be the exact issue, turns out the OSInfo class I was using is causing some error. its time to look into that and figure out what's up with it and why it isn't throwing an error! :P – dab Nov 08 '11 at 18:10