Link to home
Start Free TrialLog in
Avatar of Larry Brister
Larry BristerFlag for United States of America

asked on

Test Windows Service

In Visual STudio 2010 is there a "best practice" for testing a WIndows Service?
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image


Applies to: Windows Communication Foundation
Did you post the wrong link?


When you say "test," what do you mean? Unit testing? Functional testing? etc.
Avatar of Larry Brister



I'm making an HTTP Listener on a WIndows Service

What is the best way to go about doing this and being able to test?

Yes did not notice that.
Have you already developed the service?

I installed it but can't figure out how to see if the ReportListener() Sub is actually doing anything

The other parts are writing to file

Imports System.Net
Imports System.Threading
Imports System.Xml
Imports System.IO

Public Class ngListener
    Private StatusThread As Thread
    Private sFileName As String = "C:/temp/winServiceStart.txt"

    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.
        FileWriteline(sFileName, "Started: " & Now().ToString())

        StatusThread = New Thread(New ThreadStart(AddressOf ReportListener))
    End Sub

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

    Public Sub ReportListener()
        Dim listener As New HttpListener()
            While True
                Dim result As IAsyncResult = listener.BeginGetContext(New AsyncCallback(AddressOf ListenerGetContextCallback), listener)
                FileWriteline(sFileName, "Made it to line 32")
            End While
        Catch generatedExceptionName As ThreadAbortException
            FileWriteline(sFileName, "Exception at: " & Now().ToString())
        End Try
    End Sub

    Public Sub ListenerGetContextCallback(ar As IAsyncResult)
        Dim listener As HttpListener = DirectCast(ar.AsyncState, HttpListener)
        If listener.IsListening Then
            Dim ExportURL As String = ""
            ' Call EndGetContext to complete the asynchronous operation.
            Dim context As HttpListenerContext = listener.EndGetContext(ar)
            Dim request As HttpListenerRequest = context.Request
            ' Obtain a response object.
            Dim response As HttpListenerResponse = context.Response
            ' Construct a response.
            If request.HttpMethod = "POST" Then
                    Dim d As New XmlDocument()
                    ' PROCESS YOUR XML HERE
                    FileWriteline(sFileName, d.ToString())
                Catch e As Exception
                End Try
                ' HANDLE A GET HERE
            End If
            Dim responseString As String = "<HTML><BODY>" & DateTime.Now.ToString() & "<br>" & "HTTP Method: " & request.HttpMethod.ToString() & "<br>" & "RequestURL: " & request.Url.ToString & "<br>" & "<br>" + "</BODY></HTML>"
            Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(responseString)
            ' Get a response stream and write the response to it.
            response.ContentLength64 = buffer.Length
            response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate")
            Dim output As System.IO.Stream = response.OutputStream
            output.Write(buffer, 0, buffer.Length)
            ' You must close the output stream.
        End If
    End Sub

    Public Sub FileWriteline(ByVal sFilename As String, ByVal sLineToWrite As String)
            File.AppendAllText(sFilename, sLineToWrite & vbNewLine)
        Catch ex As Exception
        End Try
    End Sub
End Class

Open in new window

For what you are talking about, you would want to add in some logging to a file, or perhaps to the event log.

It is possible to run your application as a console application for testing, also. You just switch the type of project in the project properties from library to console application. Then just add in a Main that calls your OnStart method. You may need to put some code in Main to force it to block while you thread runs.

You might want to move your listener.Start() line inside of the Try/Catch.
I'll try those exceptions.

Just when I was getting fairly comfortable in "regular" .Net they want me to do things completely outside my ex[erience.
  I'm not able to tell if I'm getting insode the following Sub()
 Public Sub ListenerGetContextCallback(ar As IAsyncResult)

I can write to a text file inside the ReportListener() Sub

In the WHile True...
 I can write to file before and after this line
Dim result As IAsyncResult = listener.BeginGetContext(New AsyncCallback(AddressOf ListenerGetContextCallback), listener)

How would I output to file the result variable?
Avatar of HaiFai
Flag of Finland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial