windows 如何使用visual basic.net express edition安装Windows服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1870322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:33:41  来源:igfitidea点击:

How can a Windows service be installed with visual basic.net express edition?

.netwindowsvb.netservicevisual-studio-express

提问by davidcl

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.

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

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

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



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.

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

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.

这段代码的主要内容是一个名为 sampleListener 的 TCP 侦听器类。如果我将 sampleListener 类设置为启动对象并运行我的项目,它作为控制台应用程序运行良好。

There's a service class (below) which simply starts the 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

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:

对此运行 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.

This looks like a security problem, but I'm running in a cmd window that was opened using Run As Administrator.

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

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.

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

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)

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

回答by Nicky Waites

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.

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

I normally handle Install/Uninstall with a bat file.

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

Add two references to the project

添加两个对项目的引用

System.ServiceProcess
System.Configuration.Install

System.ServiceProcess
System.Configuration.Install

Service1.vb

服务1.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

项目安装程序.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

希望这对你有用

回答by Jim W

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.

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

Can you post your service class (or at least enough of it so we can see your code)?

你能发布你的服务类吗(或者至少有足够的内容以便我们可以看到你的代码)?