Find this post interesting? Do you like interesting things? Maybe you would like my invention, a connectible candle called a WickBrick!
Get one here http://wickbrick.etsy.com/
One of the drawbacks to using VB 2008 express is that some of the functionality is removed. This is perfectly fair, I mean you can download and use it for free!
Well how do you go about creating a service with it then? Well this kind soul has so helpfully created a template that you can load into visual studio.
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4511&lngWId=10
Alternate download here http://sites.google.com/site/mellerbeck/Home/Windows_Se1973412152006.zip?attredirects=0
Ok, so the trick with that is to place the zip file under “My Documents\Visual Studio 2005\Templates\ProjectTemplates\C:\Documents and Settings\Luiz.BRASVALOR\Meus documentos\Visual Studio 2005\Templates\ProjectTemplates\Visual Basic”
Now, when you go to new projects you should be able to see the Windows Service as a template. So select that, now for a quick test you could follow this example
http://www.dotheweb.net/articles/dotnet/services.aspx
Basically add a timer (Right clicking on the Toolbox, and Choose Items, add System.Timers.Timer) Do not use the forms timer!
Add the Following Code to the Timer1_Elapsed:
If Not EventLog.SourceExists(“MyApp1”) Then
EventLog.CreateEventSource(“MyApp1”, “Application”)
End If
EventLog1.Source = “MyApp1”
EventLog1.WriteEntry(“This is a simple event log entry”)
End Sub
And For OnStart():
‘ Add code here to start your service. This method should set things
‘ in motion so your service can do its work.
Timer1.Enabled = True
End Sub
Here is where you have to take a different path though, since we are using Express we can’t go right click add installer. Instead, as it shows here http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceinstaller(VS.71).aspx
You need to add your own installer code. So, right click on your project in the solutions explorer and go add new class.
Then copy and paste this in.
Imports System
Imports System.Collections
Imports System.Configuration.Install
Imports System.ServiceProcess
Imports System.ComponentModel
<RunInstallerAttribute(True)> _
Public Class MyProjectInstaller
Inherits Installer
Private serviceInstaller1 As ServiceInstaller
Private processInstaller As ServiceProcessInstaller
Public Sub New()
‘ Instantiate installers for process and services.
processInstaller = New ServiceProcessInstaller()
serviceInstaller1 = New ServiceInstaller()
‘ The services will run under the system account.
processInstaller.Account = ServiceAccount.LocalSystem
‘ The services will be started manually.
serviceInstaller1.StartType = ServiceStartMode.Manual
‘ ServiceName must equal those on ServiceBase derived classes.
serviceInstaller1.ServiceName = “Your Service”
‘ Add installers to collection. Order is not important.
Installers.Add(serviceInstaller1)
Installers.Add(processInstaller)
End Sub
End Class
The only important thing that you might need to change is the ServiceName = “Your Service” change to what ever you want your service to be called.
Ok, build your service.
Now you need to install your service. One way is to use the installutil.exe, this is located in
C:\WINDOWS\Microsoft.NET\Framework\ your dot net version here \ InstallUtil.exe
So run InstallUtil.exe and pass it the path to your compiled service. If all goes well if you go start, run, services.msc, your should see what ever you named your service show up. Go ahead and start your service and you should see sample messages appear in your event log.
Now, my newest favorite method of installing services is using an undocumented method call
This post talks about it http://anotherlab.rajapet.net/2006/06/self-installing-services-in-net.html
Add a reference to System.Configuration.Install and then
Imports System.Configuration.Install
Just create a form with two buttons and then paste
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
System.Configuration.Install.ManagedInstallerClass.InstallHelper(New String() {“C:\Test\YourService1.exe”})
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
System.Configuration.Install.ManagedInstallerClass.InstallHelper(New String() {“/u”, “C:\Test\YourService1.exe”})
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Then you can just click on the buttons to install and remove your service!
Filed under: Uncategorized | Tagged: InstallUtil.exe, Services, Visual Basic 2008 Express | 12 Comments »