我有以下代码,但我可以访问属性并检索值
this.Context.Parameters["SERVICENAME"]
这些数据在哪里,它是如何被删除的,我在哪里可以找到每种方法的顺序细分以及在哪里传递的数据?
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public string ServiceName { get; protected set; }
/// <summary>
///
/// </summary>
public ProjectInstaller()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="savedState"></param>
protected override void OnBeforeInstall(IDictionary savedState)
{
base.OnBeforeInstall(savedState);
this.ServiceName = this.Context.Parameters["SERVICENAME"].ToString();
this.serviceInstaller1.ServiceName = this.ServiceName;
this.serviceInstaller1.DisplayName = this.ServiceName;
}
/// <summary>
/// /
/// </summary>
/// <param name="savedState"></param>
protected override void OnCommitted(IDictionary savedState)
{
base.OnCommitted(savedState);
string targetDirectory = Path.GetDirectoryName(Context.Parameters["AssemblyPath"]); ;
string path = System.IO.Path.Combine(targetDirectory, "Services.Win32.exe.config");
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.Load(path);
System.Xml.XmlNode node = xDoc.SelectSingleNode("/configuration/applicationSettings/Services.Win32.Properties.Settings/setting[@name='TaskManagerServiceName']/value");
node.InnerText = (this.ServiceName); // here this.ServiceName is "" so was this.Context.Parameters[""SERVICENAME"] when i was using that
xDoc.Save(path);
}
答案 0 :(得分:3)
在尝试向现有部署项目添加其他参数时,我遇到了这个问题。参数已传递给安装程序,但在Context.Parameters中不可见。事实证明,可以访问的参数需要添加到该自定义操作的“自定义操作数据”中。
您可以通过右键单击.vdproj项目并选择“查看 - &gt;自定义操作”来实现此目的。从那里,您可以找到自定义操作的主要输出。通过在所需步骤(安装,提交,回滚或卸载)中右键单击主输出并选择属性,您可以编辑该步骤的“自定义操作数据”。您可以找到该属性here的格式。
希望能节省时间,因为我花了很长时间才弄清楚这一点。
答案 1 :(得分:1)
最简单,最干净,最强大的解决方案是不使用安装程序类自定义操作来安装服务。使用Windows Installer内置的mechansim:ServiceInstall表。
问题是您可能正在使用不公开此功能的Visual Studio部署项目。没问题。使用Windows Installer XML创建封装XE / Service组件的合并模块。然后将此合并模块添加到VDPROJ安装程序。
有关如何连线的建议,请参阅以下文章:
Augmenting InstallShield using Windows Installer XML - Certificates
Augmenting InstallShield using Windows Installer XML - Windows Services
答案 2 :(得分:0)
可以在Installer类的Install和Uninstall重写上访问它:
这里有一个完整的例子:
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
public override void Install(System.Collections.IDictionary stateSaver)
{
UpdateServiceName();
base.Install(stateSaver);
}
public override void Uninstall(System.Collections.IDictionary savedState)
{
UpdateServiceName();
base.Uninstall(savedState);
}
private void UpdateServiceName()
{
var serviceName = Context.Parameters["servicename"];
//if service name was set has install parameter then override the value set on the designer:
if (!string.IsNullOrEmpty(serviceName))
{
//serviceInstaller1 is the `ServiceInstaller` component drag to install designer.
this.serviceInstaller1.ServiceName = serviceName;
//Since user can change service name and do multiple side installs, by keep the display names similar it can help to keep those side installs packed in Service Management Console.
this.serviceInstaller1.DisplayName = $"{this.serviceInstaller1.DisplayName} ({serviceName})";
}
}
}