Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 服务正在运行时无法更改服务名称_.net_Vb.net - Fatal编程技术网

.net 服务正在运行时无法更改服务名称

.net 服务正在运行时无法更改服务名称,.net,vb.net,.net,Vb.net,我正在尝试启动服务,但出现以下错误: 服务无法启动。System.InvalidOperationException:无法执行 在服务运行时更改服务名称。在 System.ServiceProcess.ServiceBase.set_ServiceName(字符串值)位于 中的Bar.Bar.InitializeComponent() \工作组.net projects\Bar\Bar\Bar.Designer.vb:at的第47行 中的Bar.Bar.OnStart(字符串[]args) \工

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

服务无法启动。System.InvalidOperationException:无法执行 在服务运行时更改服务名称。在 System.ServiceProcess.ServiceBase.set_ServiceName(字符串值)位于 中的Bar.Bar.InitializeComponent() \工作组.net projects\Bar\Bar\Bar.Designer.vb:at的第47行 中的Bar.Bar.OnStart(字符串[]args) \工作组.net projects\Bar\Bar\Bar.vb: System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(对象 陈述

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

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
初始化组件
功能:

'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
组件设计器要求的“代码>” 作为System.ComponentModel.IContainer的专用组件 '注意:组件设计器需要以下步骤 '可以使用组件设计器对其进行修改。 '不要使用代码编辑器修改它。 _ 私有子初始化组件() “酒吧” Me.ServiceName=“Bar” 端接头
我在designer中注释掉了这一行,从而解决了问题:

<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    'Me.ServiceName = "Bar"
End Sub
_
私有子初始化组件()
'Me.ServiceName=“Bar”
端接头
InitializeComponent()方法调用的位置不正确。使用Windows服务项目模板时很难出错,Service1.cs源文件中自动生成的代码如下所示:

    public Service1() {
        InitializeComponent();
    }
换句话说,服务在构造函数运行时获得其名称。这种情况很早就发生了,Program.cs中自动生成的代码如下所示:

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

new Service1()//它在堆栈跟踪中告诉您..
Me.ServiceName=“Bar”
是否为issue@AFriend谢谢,但这是在designer上,我不能删除这一行,因为注释表明:不要使用代码编辑器修改它。不是100%确定,但如果覆盖初始化组件,是否应该在自己的代码之前调用
Base.OnStart
?这可能还包括调用
InitialiseComponent
事件将按正确的顺序触发。@AJD它工作到几天前,您不必对服务调用
Base.OnStart
。@user3378165:“它工作到几天前…”.你不认为这一点信息很重要吗?过去几天发生了什么变化?非常感谢你解释得很好的答案。