c# - 如何在启动 Windows 服务时读取 Windows 服务配置?

标签 c# .net windows-services

我无法从我的 Windows 服务的配置文件 (MyService.exe.config) 中读取 appSettings。请注意服务已成功安装。

  [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
        public class MyService : ServiceBase
        {

            public MyService()
            {
                InitializeComponent();
                ServiceName = ConfigurationManager.AppSettings.Get("ServiceName");
            }

            private void InitializeComponent()
            {
                try
                {
                                    AutoLog = true;
                    CanStop = true;
                }
                catch (Exception e)
                {
                                  // Log error
                }
            }

            static void Main()
            {

                        MyService myService = new MyService ();
                        Run(myService);               
            }

                    protected override void OnStart(string[] args)
            {
                // Code to do necessary things on start
            }
    }

事件查看器中的异常是System.Configuration.ConfigurationErrorsException

读取 Windows 服务配置的正确位置是什么? ConfigurationManager.AppSettings 始终返回 null

最佳答案

异常说明你的配置文件有问题。仔细检查。异常或其内部异常中应该有更多信息,这将为您提供更准确的错误指示。

关于c# - 如何在启动 Windows 服务时读取 Windows 服务配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5594053/

相关文章:

c# - NHibernate 2.1 示例?

ruby-on-rails - 将 delay_job 作为 Windows 服务运行

NHibernate session (和无状态 session )和长时间运行的应用程序

c# - wpf-单击按钮时动态创建新文本框和文本 block 并将其添加到网格行

c# - 将 C# 中的字符串与 if 语句中的 OR 进行比较

c# - Dotnet Core 3.1 升级错误 - 方法 'GetValidationVisitor' 没有实现

c# - .NET Framework 4.7.1 编译的应用程序是否可以与 .NET Framework 4.7.0 一起运行?

c# - 如何获取类的枚举属性列表?

c# - 通过 .NET 源中的公式将数据提取到 Excel 电子表格中的适当技术是什么?

c#-4.0 - Topshelf 窗口服务在尝试启动服务时出现错误 1053