Windows serices in VB.NET standard

russgreen

Freshman
Joined
Feb 6, 2003
Messages
27
Location
UK
Does anyone know of a macro or way of enabling Windows service projects to be created in VB.NET standard? Thomas Erdösi has written a fantastic macro to enable new projects to be compiled to dll's.

If not the would someone be kind enough to create a new blank windows service project for me?

Russ
 
You can create a service by inheriting your class from
Visual Basic:
Inherits System.ServiceProcess.ServiceBase

then you will need those in the class:

Visual Basic:
    Protected Overrides Sub OnStart(ByVal args() As String)

            End Sub

    Protected Overrides Sub OnStop()

    End Sub
 
Great thanks,

I've just done that, created a service and installer and installed my service. It show up in the list of services now but when trying to start it it takes too long and the status is stuck on starting.

Any ideas?

Russ
 
It seems to work OK but as you probably noticed there are some lines of code commented out that should write to a log file. When I uncomment this code it fails to write to the log so I'm not really sure if its doing its job. I made a console version which works well so I know the main bulk of the code is OK but this is my first go at programming a service.

Russ
 
It doesnt show anything in the file?
Try using:
Visual Basic:
sw.Flush()
After the sw.WriteLine
Also try closing the stream right after flushing without checking if its nothing.
 
I have narrowed down the problem.

:D I get my service working fine now with the on start

Visual Basic:
Dim watcherThread As Thread = New Thread(AddressOf listener)

Protected Overrides Sub OnStart(ByVal args() As String)
        WriteToLog("logs\webproxy.log", "Service started")
        watcherThread.Start()
    End Sub

the listener code is this

Visual Basic:
   Private Sub listener()
        Dim tcplistener As New TcpListener(port)
        tcplistener.Start()

        WriteToLog("logs\webproxy.log", "Listening on port " & port.ToString)

        'While True
        ' Dim socket As Socket = tcplistener.AcceptSocket()
        '  Dim webproxy As New WebProxy(socket)
        '   Dim thread As New Thread(New ThreadStart(AddressOf webproxy.runproxy))
        '    thread.Start()
        'End While
    End Sub

:confused: The problem code has been commented out. How else can I continuously monitor the tcp port for sockets?

:mad: My WriteToLog isn't working in a service either. Any ideas? It works in all my winforms and console apps.

:confused: Is there a service equivalent of Application.Startuppath?

Visual Basic:
    Public Sub WriteToLog(ByVal Filename As String, ByVal Comment As String)
        Dim sw As System.IO.StreamWriter
        Try
            sw = New System.IO.StreamWriter(Filename, True)
            sw.WriteLine(Date.Now & "     " & Comment)
            sw.Flush()
        Catch e As Exception
        Finally
            If Not sw Is Nothing Then sw.Close()
        End Try
    End Sub

Russ
 
I saw your readme and I don't think you should import System.Windows.Forms as your writing a service and not an interactive app.

I would try something like this first for getting the AppPath:
System.Reflection.Assembly.GetExecutingAssembly.Location

As to your problems with TCP, I don't know what to tell you. I've only used UDP in the service that I wrote and haven't had a chance to play with TCP in vb.net yet.
 
I need the refrence to Windows.Forms to interact with the desktop in order to write to the log files. I just had a go with System.Reflection.Assembly.GetExecutingAssembly.Location but it didn't work.

How does UDP perform?
 
I need the refrence to Windows.Forms to interact with the desktop in order to write to the log files.

Are you sure? I don't import Windows.Forms in my service and it writes a log file to c:\ (hardcoded) without any problems. btw, my logwrite function is similar to yours, except I open the file with .AppendText instead.

As to the path question, I'll have to dink around with that when I get into work tommorrow. There has to be a way to get it without a dependency on Windows.Forms and is suitable for use in a service. Another to note is that the working folder for a service maynot be what you expect it to be. I wouldn't be suprised if it always showed up as c:\winnt\system32 or something.

UDP seems to be performing fairly well, but I haven't rolled out the solution quite yet. It will eventually be installed on 1500 computers and what it does is track idle/active intervals and reports it to a stats server. Basically, if there was any input in the last 5min interval it will report "active" otherwise it sends "idle". The actual UDP packet contains the computer name, interval time, and status. This information is then going to be used for future budgeting decisions. So if a dept asks for more computers we can come back and say "You have 20 computers that are on average idle 6 hours a day. Why do you need more?"

Anyways, I have the client service installed on around 50 computers so far and they have been sending stats to the server service for the last 2 months now without fail. The only thing preventing me from doing the final roll out is waiting on Operations next phase.
 
The alternate way of getting the apppath does work as I just checked it.

System.Reflection.Assembly.GetExecutingAssembly.Location
returns "d:\#dotnet\vocompusageserver\bin\vocompusageserver.exe"

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location)
returns "d:\#dotnet\vocompusageserver\bin"
 
Back
Top