Visual Studio在服务项目中缺少“添加安装程序”链接

6
我正在构建一个Windows服务,并遵循这篇MSDN文章,但我在“创建安装程序”下的步骤3中遇到了困难。我找不到它所指的“添加安装程序”的链接。我已经尝试了所有方法,包括按照它给出的指示进行操作,但似乎无法找到它。一些人在谷歌上也遇到了同样的问题,但从未找到解决方案(除了手动添加ServiceInstaller对象并进行配置)。
是否有其他人遇到过这个问题并找到了原因?如果有影响,我正在使用VS2008并针对.Net 2.0。
4个回答

7
他们所谈论的“灰色区域”是属性面板中命令面板(不是笔误)。它并不是很有用,所以您可能已经关闭了它,我也是这样做的。
您可以通过右键单击属性面板并选择“命令”来重新启用它,或者直接通过右键单击服务设计视图(带有“要向类添加组件...”的大棕色窗口)并选择“添加安装程序项目”来添加安装程序项目。

2
仅供记录,这完全是胡说八道,它默认隐藏(或者MSDN文档不够清晰)是荒谬的。你在听吗,微软? - SqlRyan
@SqlRyan 没错,这让我彻底疯了。 - Contango
1
我正在使用VS2012,但右键单击灰色区域时看不到“添加安装程序”的选项... - Tobia

4

为了跟上新的Visual Studio Express(2015)版本:

似乎在Express版本中我们无法使用"添加安装程序"。但是实际上很简单。您只需创建一个类并添加以下代码即可。

此外,您需要添加引用System.Configuration.Install.dll。

using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel;


namespace SAS
{
    [RunInstaller(true)]
    public class MyProjectInstaller : Installer
    {
        private ServiceInstaller serviceInstaller1;
        private ServiceProcessInstaller processInstaller;

        public MyProjectInstaller()
        {
            // Instantiate installer for process and service.
            processInstaller = new ServiceProcessInstaller();
            serviceInstaller1 = new ServiceInstaller();

            // The service runs under the system account.
            processInstaller.Account = ServiceAccount.LocalSystem;

            // The service is started manually.
            serviceInstaller1.StartType = ServiceStartMode.Manual;

            // ServiceName must equal those on ServiceBase derived classes.
            serviceInstaller1.ServiceName = "SAS Service";

            // Add installer to collection. Order is not important if more than one service.
            Installers.Add(serviceInstaller1);
            Installers.Add(processInstaller);
        }
    }
}

3
对于Visual Studio 2012,右键单击“Services1.cs”,选择“查看设计器”(或按Shift-F7)。然后,在设计器的灰色背景上右键单击。
只有这样,您才能看到微软一直隐藏着的彩蛋:难以捉摸的“添加安装程序”链接。 enter link description here

1
我这里没有添加安装程序的选项。 - regularjoe
你右键单击灰色背景,在文本 To add components to your class... 上面了吗? - Contango

0

检查您尝试添加安装程序的 .cs 文件是否扩展了 System.ServiceProcess.ServiceBase 而不是 System.ComponentModel.Component

要将 .cs 文件作为代码而不是在设计器中打开,请在“解决方案资源管理器”中选择该文件,然后按 F7 键或右键单击它并选择“查看代码”。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接