如何使用 Visual Basic.net Express 版本安装 Windows 服务?

发布于 2024-08-14 09:46:29 字数 2910 浏览 3 评论 0原文

编辑:我正在对这个问题开始悬赏。目前,我已经继续使用 VS2010 Pro Beta 开发我的应用程序,但我真的希望它能够使用 Express 版本构建,因为我们通常不是 .net 商店,即使其中一个或两个开发人员拥有 VS PRO,但我们整个团队无法使用它。

要成为接受的答案并领取赏金,您必须提供示例代码和说明,以允许使用 vb 2008 Express 版本安装和卸载 Windows 服务。您不一定需要从我的代码开始(但它的要点包含在下面)。


我编写了一个 VB.NET 应用程序,我想将其作为服务运行。目前我使用的是VB.net Express Edition(2008),它不附带“Service”模板,但我添加了一个Service类(继承自ServiceBase)和一个Installer类(继承自Installer);在这两种情况下,我都遵循 MSDN 中的示例代码。不幸的是,我无法将此代码安装并作为服务运行。

这段代码的核心是一个名为sampleListener 的TCP 侦听器类。如果我将 SampleListener 类设置为启动对象并运行我的项目,它可以作为控制台应用程序正常运行。

有一个服务类(如下),它只是启动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

还有一个安装程序类,我认为这是我的问题的根源。这是我最初编写的安装程序类。

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

对此运行 installutil.exe 会产生以下消息:

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.

这看起来像是一个安全问题,但我正在使用以管理员身份运行打开的 cmd 窗口中运行。

我根据在线示例尝试了一个大大简化的安装程序类:

Imports System.ComponentModel
Imports System.Configuration.Install

<RunInstaller(True)> Public Class ProjectInstaller
    Inherits System.Configuration.Install.Installer

End Class

这看起来非常简单,我无法弄清楚它如何工作,事实上它没有。但是,当在具有此版本的安装程序类的项目上运行 installutil.exe 时,installutil.exe 不会抛出错误消息并报告服务已成功安装。

我怀疑我的安装程序类中需要代码来执行第一个示例中的一些操作,但不执行导致错误的部分。

有什么建议吗?

(为了清晰起见,对此进行了广泛的编辑,并添加了最初未包含的代码示例)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吾家有女初长成 2024-08-21 09:46:29

这似乎对我有用,但我没有添加您自己的代码。

创建两个文件 Service1.vb 和 ProjectInstaller.vb。通常,Service1 和 ProjectInstaller 被设置为部分类,但为了在这里发布,它们不是。我不认为它有任何副作用,但其他人可以对此发表评论。

我通常使用 bat 文件来处理安装/卸载。

添加两个对项目

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"

希望这对您有用

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

落墨 2024-08-21 09:46:29

你可以做到,但我对你的解释有点困惑。您是否安装了该服务,或者是否尝试将该服务作为控制台应用程序运行?您需要安装该服务,以便注册它并从服务管理器运行它。或者,您可以构建一个“main”方法来运行 onstart 方法中的代码,但您不能通过仅将服务类设置为启动方法来在调试模式下调用 onstart/onstop/pause 等方法。

您可以发布您的服务类(或者至少足够的服务类,以便我们可以看到您的代码)吗?

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)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文