我正在尝试启动服务,但出现了Followig错误:
服务无法启动。 System.InvalidOperationException:无法 服务运行时更改服务名称。在 System.ServiceProcess.ServiceBase.set_ServiceName(字符串值)在 Bar.Bar.InitializeComponent()在 \ workgroup.net projects \ Bar \ Bar \ Bar.Designer.vb:第47行位于 Bar.Bar.OnStart(String [] args)在 \ workgroup.net projects \ Bar \ Bar \ Bar.vb:第22行位于 System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object 状态
我根本不打算更改服务名称!!!是什么导致错误?
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
答案 0 :(得分:2)
InitializeComponent()方法调用放置不正确。使用Windows Service项目模板时很难出错,Service1.cs源文件中的自动生成的代码如下所示:
public Service1() {
InitializeComponent();
}
换句话说,服务在构造函数运行时获得其名称。这很早就发生了,Program.cs中自动生成的代码如下:
static void Main() {
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1() // <=== here
};
ServiceBase.Run(ServicesToRun);
}
换句话说,在程序启动时,立即开始与OS交互并已承诺使用服务名称。 OnStart()太晚了,它是操作系统的回调。
因此,请验证构造函数和Main()入口点仍然看起来不错。然后从OnStart()删除呼叫。
答案 1 :(得分:0)
我最终在设计器中注释了这一行,并解决了该问题:
StretchRect