EDIT: I'm starting a bounty on this question. For the moment, I've moved on and am developing my application using VS2010 Pro Beta, but I'd really like it to be able to be built with express edition, since we are generally not a .net shop and even if one or two developers have VS PRO it will not be available to our entire team.
To be the accepted answer and claim the bounty, you must provide sample code and instructions that will allow a Windows Service to be installed and uninstalled using vb 2008 express edition. You don't necessarily need to start with my code (but the essentials of it are included below).
I've written a VB.NET application which I would like to run as a service. Currently I'm using VB.net Express Edition (2008) which does not ship with the "Service" template, but I've added a Service class (inheriting from ServiceBase) and an Installer class (inheriting from Installer); in both cases I'm following sample code from MSDN. Unfortunately I haven't been able to get this code to install and run as a service.
The meat of this code is a TCP Listener class called sampleListener. If I set the sampleListener class as the startup object and run my project, it runs fine as a console application.
There's a service class (below) which simply starts the sampleListener.
Public Class sampleSocketService
Inherits System.ServiceProcess.ServiceBase
Public Sub New()
Me.ServiceName = "sample Socket Service"
Me.CanStop = True
Me.CanPauseAndContinue = True
Me.AutoLog = True
End Sub
Shared Sub Main()
System.ServiceProcess.ServiceBase.Run(New sampleSocketService)
End Sub
Protected Overrides Sub OnStart(ByVal args() As String)
sampleListener.Main()
End Sub
End Class
There's also an installer class which I think is the source of my problems. Here's the installer class as I initially wrote it.
Imports System
Imports System.Collections
Imports System.Configuration.Install
Imports System.ServiceProcess
Imports System.ComponentModel
<RunInstallerAttribute(True)> _
Public Class sampleSocketServiceInstaller
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()
processInstaller.Account = ServiceAccount.LocalSystem
serviceInstaller1.StartType = ServiceStartMode.Automatic
' ServiceName must equal those on ServiceBase derived classes.
serviceInstaller1.ServiceName = "sample Socket Service"
开发者_运维百科 ' Add installers to collection. Order is not important.
Installers.Add(serviceInstaller1)
Installers.Add(processInstaller)
End Sub
End Class
Running installutil.exe on this produces the following message:
An exception occurred during the Install phase.
System.Security.SecurityException: The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.
This looks like a security problem, but I'm running in a cmd window that was opened using Run As Administrator.
I tried a much simplified Installer class based on an online example:
Imports System.ComponentModel
Imports System.Configuration.Install
<RunInstaller(True)> Public Class ProjectInstaller
Inherits System.Configuration.Install.Installer
End Class
This seems ridiculously simple, and I couldn't figure out how it could possibly work, and in fact it didn't. However, when running installutil.exe on the project with this version of the installer class, installutil.exe does not throw an error message and reports that the service has been installed successfully.
I suspect I need code in my installer class that does some of what's in my first example, but doesn't do whichever part is causing the error.
Any suggestions?
(This has been edited extensively for clarity and to add code examples which were not originally included)
This seems to work for me but I haven't added in your own code.
Create two files a Service1.vb and a ProjectInstaller.vb. Normally the Service1 and ProjectInstaller are setup as Partial classes but for the sake of posting here they are not. I dont think it has any side affects but someone else can comment on that.
I normally handle Install/Uninstall with a bat file.
Add two references to the project
System.ServiceProcess
System.Configuration.Install
Service1.vb
Imports System.ServiceProcess
Public Class Service1
Inherits System.ServiceProcess.ServiceBase
Protected Overrides Sub OnStart(ByVal args() As String)
End Sub
Protected Overrides Sub OnStop()
End Sub
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
<MTAThread()> _
<System.Diagnostics.DebuggerNonUserCode()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
Private components As System.ComponentModel.IContainer
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
Me.ServiceName = "Service1"
End Sub
End Class
ProjectInstaller.vb
Imports System.ComponentModel
Imports System.Configuration.Install
<System.ComponentModel.RunInstaller(True)> _
Public Class ProjectInstaller
Inherits System.Configuration.Install.Installer
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
Private components As System.ComponentModel.IContainer
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.ServiceProcessInstaller1 = New System.ServiceProcess.ServiceProcessInstaller
Me.ServiceInstaller1 = New System.ServiceProcess.ServiceInstaller
Me.ServiceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem
Me.ServiceProcessInstaller1.Password = Nothing
Me.ServiceProcessInstaller1.Username = Nothing
Me.ServiceInstaller1.ServiceName = "Service1"
Me.ServiceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic
Me.Installers.AddRange(New System.Configuration.Install.Installer() {Me.ServiceProcessInstaller1, Me.ServiceInstaller1})
End Sub
Friend WithEvents ServiceProcessInstaller1 As System.ServiceProcess.ServiceProcessInstaller
Friend WithEvents ServiceInstaller1 As System.ServiceProcess.ServiceInstaller
End Class
Install Bat
C:
CD \WINDOWS\Microsoft.NET\Framework\v2.0.50727
installutil "C:\Visual Studio 2008\Projects\....\Temp.exe"
pause
NET START Service1
Unstall Bat
C:
CD \WINDOWS\Microsoft.NET\Framework\v2.0.50727
NET STOP Service1
installutil /u "C:\Visual Studio 2008\Projects\....\Temp.exe"
Hopefully that'll work for you
Have a look at this thread:
http://social.msdn.microsoft.com/Forums/en/windowsgeneraldevelopmentissues/thread/416098a4-4183-4711-a53b-e10966c9801d
You can do it, but I'm a bit confused by your explanation. Did you install the service or are you trying to run the service as a console app? You need to install the service so it's registered and run it from the service manager. Or you can build a "main" method that runs the code within your onstart method, but you can't call onstart/onstop/pause etc methods in debug mode by just setting your service class as the startup method.
Can you post your service class (or at least enough of it so we can see your code)?
精彩评论