• C#实现windows服务安装,服务名可配置时出问题(无法创建 ProjectInstaller 安装程序类型的实例)


     【参考资料】Windows 安装服务 的两种方式

    一、问题出在System.Configuration.ConfigurationManager.AppSettings[""].ToString()无法读取 config文件配置

     二、ProjectInstaller.Designer.cs源码

    using System.Xml;
    
    namespace ServiceHost
    {
        partial class ProjectInstaller
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary> 
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region 组件设计器生成的代码
    
            /// <summary>
            /// 设计器支持所需的方法 - 不要修改
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
                this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller()
                {
                    Description = "Description ",
                    DisplayName = Get_ConfigValue(this.GetType().Assembly.Location + ".config", "JobDisplayName"),//System.Configuration.ConfigurationManager.AppSettings["JobDisplayName"].ToString(),
                    ServiceName = Get_ConfigValue(this.GetType().Assembly.Location + ".config", "JobServiceName"),//System.Configuration.ConfigurationManager.AppSettings["JobServiceName"].ToString(),
                    StartType = System.ServiceProcess.ServiceStartMode.Automatic
                };
                this.serviceInstaller2 = new System.ServiceProcess.ServiceInstaller()
                {
                    Description = "Description ",
                    DisplayName = Get_ConfigValue(this.GetType().Assembly.Location + ".config", "InterfaceDisplayName"), //System.Configuration.ConfigurationManager.AppSettings["InterfaceDisplayName"].ToString(),
                    ServiceName = Get_ConfigValue(this.GetType().Assembly.Location + ".config", "InterfaceServiceName"),//System.Configuration.ConfigurationManager.AppSettings["InterfaceServiceName"].ToString(),
                    StartType = System.ServiceProcess.ServiceStartMode.Automatic
                };
                // 
                // serviceProcessInstaller1
                // 
                this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
                this.serviceProcessInstaller1.Password = null;
                this.serviceProcessInstaller1.Username = null;
    
                // 
                // ProjectInstaller
                // 
                this.Installers.AddRange(new System.Configuration.Install.Installer[] {
                this.serviceProcessInstaller1,
                this.serviceInstaller1,
                this.serviceInstaller2});
    
            }
    
            #endregion
    
            private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
            private System.ServiceProcess.ServiceInstaller serviceInstaller1;
            private System.ServiceProcess.ServiceInstaller serviceInstaller2;
    
            protected static string Get_ConfigValue(
    style="color: #0000ff;">string configpath, string strKeyName)
            {
                using (XmlTextReader tr = new XmlTextReader(configpath))
                {
                    while (tr.Read())
                    {
                        if (tr.NodeType == XmlNodeType.Element)
                        {
                            if (tr.Name == "add" && tr.GetAttribute("key") == strKeyName)
                            {
                                return tr.GetAttribute("value");
                            }
                        }
                    }
                }
                return "";
            }
        }
    
    }
  • 相关阅读:
    smarty对网页性能的影响
    php-fpm正在生成页面时,浏览器刷新后,php-fpm会退出吗?
    为什么日志记录到别的目录了?
    一个空格引发的bug
    linux内核--页高速缓存
    radix树
    linux内核--用户态内存管理
    linux内核--内核内存管理
    linux内核--软中断与tasklet
    linux内核--几个上下文(context)
  • 原文地址:https://www.cnblogs.com/xdot/p/7448857.html
Copyright © 2020-2023  润新知