Solved

Test Windows Service

Posted on 2013-05-14
12
251 Views
Last Modified: 2013-05-15
In Visual STudio 2010 is there a "best practice" for testing a WIndows Service?
0
Comment
Question by:lrbrister
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39165135
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39165151
@CodeCruiser

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

@Irbrister

When you say "test," what do you mean? Unit testing? Functional testing? etc.
0
 

Author Comment

by:lrbrister
ID: 39165239
kaufmed

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?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39165459
@kaufmed

Yes did not notice that.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39165807
Have you already developed the service?
0
 

Author Comment

by:lrbrister
ID: 39165822
kaufmed,
 Yes.

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))
        StatusThread.Start()
    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()
        listener.Prefixes.Add("http://+:60010/")
        listener.Start()
        Try
            While True
                Dim result As IAsyncResult = listener.BeginGetContext(New AsyncCallback(AddressOf ListenerGetContextCallback), listener)
                result.AsyncWaitHandle.WaitOne()
                FileWriteline(sFileName, "Made it to line 32")
            End While
        Catch generatedExceptionName As ThreadAbortException
            FileWriteline(sFileName, "Exception at: " & Now().ToString())
            listener.Close()
        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
                Try
                    Dim d As New XmlDocument()
                    ' PROCESS YOUR XML HERE
                    d.Load(request.InputStream)
                    FileWriteline(sFileName, d.ToString())
                Catch e As Exception
                End Try
                ' HANDLE A GET HERE
            Else
            End If
            ' CONSTRUCT A RESPONSE
            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.
            output.Close()
        Else
        End If
    End Sub


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

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39165860
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.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39165874
P.S.

You might want to move your listener.Start() line inside of the Try/Catch.
0
 

Author Comment

by:lrbrister
ID: 39165892
kaufmed
I'll try those exceptions.

Just when I was getting fairly comfortable in "regular" .Net projects...now they want me to do things completely outside my ex[erience.
0
 

Author Comment

by:lrbrister
ID: 39166186
kaufmed,
  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?
0
 
LVL 7

Accepted Solution

by:
HaiFai earned 500 total points
ID: 39167455
Im currently testin(debugging)  my services like this
1 make installer to project
2 click installer in visual studio and install service
3 start service
4 attach debugger to service from visual studio (tools -- attach to process)
then just check if service is running like it should and you can pause service in code to specified like or so.
5 if you need to edit code ... just click installer project and remove then build project again and start again from step 2
0
 

Author Closing Comment

by:lrbrister
ID: 39168705
Perfect
Thanks
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question