Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help getting windows service to work

Status
Not open for further replies.

majordog

Programmer
Jul 8, 2002
222
0
0
CA
Hey all,
I wrote a windows app in vb.Net that I had to recently convert to a windows service. I'm hoping somebody can tell where I went wrong because the code works in the windows app but doesn't do anything as a service. Below is the relevant code...(If Imiss anything - just let me know)
Service1.vb file:
Public Sub New()
MyBase.New()

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

' Add any initialization after the InitializeComponent() call
'Add any initialization after the InitializeComponent() call
watchfolder = New System.IO.FileSystemWatcher()

'this is the path to the folder to be monitored
watchfolder.Path = "C:\ermdLocationTemperature"

'List of Filter we want to specify
'make sure you use OR for each Filter as we need to
'all of those

watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes

' Add the handler to each event - Only reacting to new files at creation
AddHandler watchfolder.Created, AddressOf logchange

'Set this property to true to start watching
watchfolder.EnableRaisingEvents = True

End Sub


Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase

' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase() {New MyNewservice()}

System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'MyNewservice
'
Me.ServiceName = &quot;MyNewService&quot;

End Sub

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.
getFolderFiles()
End Sub

Private Sub logchange(ByVal source As Object, ByVal e As _
System.IO.FileSystemEventArgs)

If e.ChangeType = IO.WatcherChangeTypes.Changed Then

End If
'If a file has been newly created then...
If e.ChangeType = IO.WatcherChangeTypes.Created Then

oRead = oFile.OpenText(e.FullPath)
While oRead.Peek <> -1
parseTextFile(oRead)
End While
oRead.Close()
If blnDelete = True Then
oFile.Delete(e.FullPath)
End If
End If
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then

End If
getFolderFiles()
End Sub

Public Sub getFolderFiles()
Dim d() As String
Dim e As System.IO.FileSystemEventArgs()
Dim location As Object = &quot;c:\ermdLocationTemperature&quot;

d = System.IO.Directory.GetFiles(location)

Dim en As System.Collections.IEnumerator

en = d.GetEnumerator

While en.MoveNext
oRead = oFile.OpenText(en.Current)
parseTextFile(oRead)
oRead.Close()
If blnDelete = True Then
oFile.Delete(en.Current)
End If
End While
End Sub
 
Hmm - I don't see anything obvious. A while ago I did something similar and decided against the FileSystemWatcher component, and went instead for polling a directory on a timed interval. So I don't know how well the FSW component works in services.

Anyway, in this post I gave some tips about working with Windows services

thread796-511928

Hope it helps

Mark [openup]
 
Try writing to an event log to get more diagnostics. Something like this might work.

try
...your code hear...
catch e as exception

Dim aLog As EventLog
aLog = New EventLog()
aLog.Source = &quot;MyServiceLog&quot;
aLog.WriteEntry(&quot;Error: &quot; & e.Message, _ EventLogEntryType.Error)
aLog.Close()
...maybe exit...
end try


Make sure you trap errors. Use try-catch-end try to catch a System.Exception


 
Hi guys,
I finally narrowed down the problem(although I had beensidetracked on something else, hence the delay). I'm hoping you may be able to give me the answer I need. I added the log viewer to the service and was able to trap what the problem was. The database connection is not opening. I checked the logs on the db (which is SQL Server 2000)and it is trying to use 'NT Authority\Anonymous Logon'. I specified in my connection string the correct username and password but it obviously take sthis info from somewhere else. So...I went looking and came across what I knew to be the area to be modified. In the component designer of the installer I modified the code with my username and password - Which doesn't work. I get &quot;The account name is invalid or does not exist, or the password is invalid for the account name specified&quot;. I know the account is good, butthat I must be referncing something in the syntax incorrectly. Do either of you know what I should change?:

Friend WithEvents ServiceProcessInstaller1 As System.ServiceProcess.ServiceProcessInstaller
Friend WithEvents MyNewService As System.ServiceProcess.ServiceInstaller
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ServiceProcessInstaller1 = New System.ServiceProcess.ServiceProcessInstaller()
Me.MyNewService = New System.ServiceProcess.ServiceInstaller()
'
'ServiceProcessInstaller1
'
Me.ServiceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService
Me.ServiceProcessInstaller1.Username = &quot;domain\myusername&quot;
Me.ServiceProcessInstaller1.Password = &quot;mypassword&quot;

'
'MyNewService
'
Me.MyNewService.ServiceName = &quot;MyNewService&quot;
Me.MyNewService.StartType = System.ServiceProcess.ServiceStartMode.Automatic
'
'ProjectInstaller
'
Me.Installers.AddRange(New System.Configuration.Install.Installer() {Me.ServiceProcessInstaller1, Me.MyNewService})

End Sub
 
HI again,
I was able to solve the problem by changing the serviceprocess account to user. So that works fine. But now there are other problems - Back to the drawing board...
 
I have everything working the way it is supopsed to now - On my development machine. One problem, I can't get the service to start on the target machine. I continuously get the error: &quot;Could not start the MyNewService service on local computer. The service did not return an error. This could be an internal windows error or an internal service error. If the problem persists, contact your system administrator.&quot; I assumed that it had something to do with the account I was using because everything is the same as it was on the development machine(except all the libraries uesd are now itemized in the dependencies of the service ). So I used the logon user admin and password for the local user group. Still, can install service but will not start....Any input from the peanut gallery??? :)
 
Your sub which is fired from the start routine, getFolderFiles, if it takes longer than 30 seconds to iterate through the files, will fool windows into thinking that the service has not started correctly, but the service will still be running. Can you see if it seems to be working, for example are the files being deleted?
Unfortunately, there is no way I know of to change this 30 second timeout. One way you might be able to prevent this error could be to use a timer control and instead of doing this folder process on start, do it when the first timer event fires.
Alternatively, when the folder processing is complete, windows will recognise that the service is running and you should be able to control it normally, even though it gave you that message.

Hope this helps
Mark

Mark [openup]
 
Hey Mark - No the service was not working at all. But I was able to track the problem to some type of hidden file in the folder that had inconsistent data which prevented the write and the service from starting. I couldn't even locate the file it was reading. I had to physically delete the folder and re-create it. I wouldn't have even found this if I wasn't able to track the actions with the event viewer. So, thanks guys - Just adding that helped tremendously! S
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top