简体   繁体   English

Windows 服务安装程序 - 服务名称包含无效字符、为空或太长

[英]Windows Service Installer - Service name contains invalid characters, is empty, or is too long

I've been following this MSDN article about writing a Windows Service and accompanying installer.我一直在关注这篇关于编写 Windows 服务和随附安装程序的 MSDN 文章 When I try to add the installer project and I right-click on the Design View and select 'Add Installer' I receive the following message:当我尝试添加安装程序项目并右键单击设计视图并选择“添加安装程序”时,我收到以下消息:

Service name contains invalid characters, is empty, or is too long

My service name is set to "UploadManagerService".我的服务名称设置为“UploadManagerService”。 This doesn't contain any invalid characters that I'm aware of and is below the specified 80 character limit.这不包含我知道的任何无效字符,并且低于指定的 80 个字符限制。

I've read several articles on here about this problem.我在这里阅读了几篇关于这个问题的文章。 This answer suggests that this is because the service name isn't set in Visual Studio though this is set in my case. This answer表明这是因为服务名称未在 Visual Studio 中设置,尽管这是在我的情况下设置的。 I've also tried setting the service name in the app.config to see if that make any difference.我还尝试在 app.config 中设置服务名称以查看是否有任何区别。 Is there something else I could be missing or a way around this?还有其他我可能会遗漏的东西或解决这个问题的方法吗?

I wasn't able to understand what the problem was with the name but I was able to get round the problem by writing the installer manually with a few lines of code.我无法理解名称有什么问题,但我可以通过使用几行代码手动编写安装程序来解决这个问题。

Assuming that you're follow the MSDN article and get the same problem after selecting 'Add Installer':假设您遵循MSDN 文章并在选择“添加安装程序”后遇到同样的问题:

  • Select 'ProjectInstaller.cs' on the Solution Explorer and hit F7 / right-click and select 'View Code'.在解决方案资源管理器中选择“ProjectInstaller.cs”,然后按 F7/右键单击并选择“查看代码”。
  • If the constructor isn't present for the class, add one or edit the existing constructor.如果类的构造函数不存在,请添加一个或编辑现有的构造函数。

Add the following code:添加以下代码:

var process = new ServiceProcessInstaller {Account = ServiceAccount.LocalSystem};
var serviceAdmin = new ServiceInstaller
{
    StartType = ServiceStartMode.Manual,
    ServiceName = "MyServiceName",
    DisplayName = "My Service Display Name"
};

Installers.Add(process);
Installers.Add(serviceAdmin);

InitializeComponent();

After this I carried on with the instructions on the MSDN article and was able to install the service with InstallUtil.exe.在此之后,我继续按照 MSDN 文章中的说明进行操作,并能够使用 InstallUtil.exe 安装该服务。

I'm still none the wiser as to why this won't work but hopefully this will help someone else get round the problem.我仍然不明白为什么这行不通,但希望这能帮助其他人解决这个问题。

I was getting this exact error and everything was supposed to be fine.我收到了这个确切的错误,一切都应该没问题。 You read the error message fast, you blame it and then start checking the project's name, names of the classes and... wait a moment:您快速阅读错误消息,责备它,然后开始检查项目名称、类名称和……等一下:

在此处输入图像描述

The MSDN tutorial names this step: MSDN 教程将此步骤命名为:

In the Design tab, select Properties from the shortcut menu.在“设计”选项卡中,从快捷菜单中选择“属性”。 From the Properties window, change the ServiceName value to MyNewService.在“属性”窗口中,将 ServiceName 值更改为 MyNewService。 https://learn.microsoft.com/en-us/dotnet/framework/windows-services/media/windows-service-properties.png

I simply didn't follow the tutorial when I built the service, so remember folks: read the error messages and check every single point it's complaining about!我只是在构建服务时没有遵循教程,所以请记住伙计们:阅读错误消息并检查它所抱怨的每一个点!

I would consider watching this:我会考虑看这个:

Create and Package a Windows Service using IsWiX使用 IsWiX 创建和打包 Windows 服务

You can really simplify the problem by eliminating the use of InstallUtil and using native MSI functionality.您可以通过取消使用 InstallUtil 并使用本机 MSI 功能来真正简化问题。 In fact, you'll notice that video only took 2 minutes and half that time is used to create the actual service.事实上,您会注意到视频只用了 2 分钟,其中一半时间用于创建实际服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM