代码之家  ›  专栏  ›  技术社区  ›  user3378165

服务运行时无法更改服务名称

  •  1
  • user3378165  · 技术社区  · 7 年前

    我正在尝试启动服务,但出现以下错误:

    无法启动服务。System.InvalidOperationException:无法 在服务运行时更改服务名称。在 system.serviceprocess.servicebase.set servicename(字符串值)at Bar.Bar.InitializeComponent()输入 \工作组.net 项目\bar\bar\bar.designer.vb:line 47 at bar.bar.onstart(string[]args)在 \工作组.net 项目\bar\bar\bar.vb:line 22 at System.ServiceProcess.ServiceBase.ServiceQueuedMinCallback(对象 状态

    我根本不想更改服务名!!!什么会导致错误?

    Protected Overrides Sub OnStart(ByVal args() As String)
        InitializeComponent()
        writeToLog("in onstart")
        folderToWatch = New FileSystemWatcher
        folderToWatch.Path = sDocDir
        With folderToWatch
            .NotifyFilter = .NotifyFilter Or NotifyFilters.FileName
            .NotifyFilter = .NotifyFilter Or NotifyFilters.Attributes
        End With
        AddHandler folderToWatch.Created, AddressOf ProcessBarCode
        folderToWatch.EnableRaisingEvents = True
    End Sub
    

    这个 InitializeComponent 功能:

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer
    
    ' NOTE: The following procedure is required by the Component Designer
    ' It can be modified using the Component Designer.  
    ' Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        'Bar'
        Me.ServiceName = "Bar"
    
    End Sub
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Hans Passant    7 年前

        public Service1() {
            InitializeComponent();
        }
    

        static void Main() {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new Service1()                    // <=== here
            };
            ServiceBase.Run(ServicesToRun);
        }
    

        2
  •  0
  •   user3378165    7 年前

    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        'Me.ServiceName = "Bar"
    End Sub