1

編集:私はこの質問に報奨金を始めています。今のところ、VS2010 Pro Betaを使用してアプリケーションを開発していますが、通常は.netショップではないため、エクスプレスエディションでビルドできるようにしたいと考えています。 2人の開発者がVSPROを使用しており、チーム全体で利用できるわけではありません。

受け入れられた回答であり、賞金を請求​​するには、vb 2008ExpressEditionを使用してWindowsサービスをインストールおよびアンインストールできるようにするサンプルコードと手順を提供する必要があります。必ずしも私のコードから始める必要はありません(ただし、その要点は以下に含まれています)。


サービスとして実行したいVB.NETアプリケーションを作成しました。現在、「Service」テンプレートに付属していないVB.net Express Edition(2008)を使用していますが、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はエラーメッセージをスローせず、サービスが正常にインストールされたことを報告します。

インストーラークラスに、最初の例の一部を実行するコードが必要だと思いますが、エラーの原因となっている部分は実行しません。

助言がありますか?

(これは、明確にするため、および元々含まれていなかったコード例を追加するために大幅に編集されています)

4

3 に答える 3

6

これは私にとってはうまくいくようですが、私はあなた自身のコードを追加していません。

Service1.vbとProjectInstaller.vbの2つのファイルを作成します。通常、Service1とProjectInstallerはPartialクラスとして設定されますが、ここに投稿するために、そうではありません。副作用はないと思いますが、他の誰かがコメントできます。

私は通常、batファイルを使用してインストール/アンインストールを処理します。

プロジェクトに2つの参照を追加します

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

バットをインストールする

C:
CD \WINDOWS\Microsoft.NET\Framework\v2.0.50727
installutil "C:\Visual Studio 2008\Projects\....\Temp.exe"
pause
NET START Service1

コウモリを解き放つ

C:
CD \WINDOWS\Microsoft.NET\Framework\v2.0.50727
NET STOP Service1
installutil /u "C:\Visual Studio 2008\Projects\....\Temp.exe"

うまくいけば、それはあなたのために働くでしょう

于 2009-12-22T11:17:22.220 に答える
0

あなたはそれをすることができます、しかし私はあなたの説明によって少し混乱しています。サービスをインストールしましたか、それともコンソールアプリとしてサービスを実行しようとしていますか?サービスをインストールして登録し、サービスマネージャから実行する必要があります。または、onstartメソッド内でコードを実行する「main」メソッドを作成することはできますが、サービスクラスをstartupメソッドとして設定するだけでは、デバッグモードでonstart / onstop/pauseなどのメソッドを呼び出すことはできません。

あなたはあなたのサービスクラスを投稿できますか(または少なくともあなたのコードを見ることができるように十分です)?

于 2009-12-08T22:53:03.877 に答える
0

このスレッドを見てください:

http://social.msdn.microsoft.com/Forums/en/windowsgeneraldevelopmentissues/thread/416098a4-4183-4711-a53b-e10966c9801d

于 2009-12-08T22:31:07.887 に答える