windows Service 读取app.config AppSettings为空


windows Service中需要读取App.config的AppSettings节点

App.config的内容为:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="ServiceName" value="GZProxyServer"/>
    <add key="DisplayName" value="GZProxyServer在线代理服务"/>
    <add key="Description" value="支持HTTP代理"/>

    <add key="ListeningIPInterface" value="192.168.40.1"/>
    <add key="ListeningPort" value="8889"/>
    <add key="CertificateFile" value="cert.cer"/>
    
  </appSettings>
</configuration>

代码中使用:

string ServiceName = System.Configuration.ConfigurationManager.AppSettings["ServiceName"];
string DisplayName = System.Configuration.ConfigurationManager.AppSettings["DisplayName"];
string Description = System.Configuration.ConfigurationManager.AppSettings["Description"];
Logs.Info($"ServiceName:{ServiceName}\r\nDisplayName:{DisplayName}\r\nDescription:{Description}");
GarsonZhang www.yesdotnet.com

得到记录日志为:


没有获取到值

解决方案

使用ConfigHelper

ConfigHelper configHelper = new ConfigHelper("");
//string ServiceName = System.Configuration.ConfigurationManager.AppSettings["ServiceName"];
//string DisplayName = System.Configuration.ConfigurationManager.AppSettings["DisplayName"];
//string Description = System.Configuration.ConfigurationManager.AppSettings["Description"];
string ServiceName = configHelper.GetAppSettingsValue("ServiceName");
string DisplayName = configHelper.GetAppSettingsValue("DisplayName");
string Description = configHelper.GetAppSettingsValue("Description");
GarsonZhang www.yesdotnet.com

结果:

已经获取到值了:

Confighelper类,参考:

http://www.yesdotnet.com/archive/post/1616443609.html

 

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
YES开发框架
上一篇:C#解析指定dll的app.config配置文件
下一篇:winform设置管理员运行
评论列表

发表评论

评论内容
昵称:
关联文章

windows Service 读取app.config AppSettings
C#解析指定dll的app.config配置文件
Windows服务程序开发
Python windows服务报错: Installing service ALicense Error installing service: 拒绝访问
.NET Core 项目 DbProviderFactories.GetFactoryClasses()返回
.NETCore和.NET5 MVC解析获取appsettings.json数据
表结构修改>新增主键或者不能的列
VS制作C#程序windows安装程序
nginx安装windows服务
.NET Core 深入理解依赖注入 services.AddTransient,services.AddScoped,services.AddSingleton
windows Redis配置
文件或目录损坏且无法读取
.net core api部署windows服务
Python Windows服务
C# LINQ中SUM,MAX,MIN时如果数据,报错NULL
迁移 dotnet 6 提示必须将目标平台设置 Windows 平台
探索 dotnet core 何在 Windows7 系统需要补丁的原因
C#判断类型是否可
C#8.0 可引用类型
window server部署open VPN

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码