在 Visual Studio 2015 中创建了一个 C# 服务项目,将其从各种代码源组合在一起。
\n\n它编译了,我可以安装该服务,并且可以启动该服务,但该服务不会\xe2\x80\x99t触发逻辑。它应该只查看特定文件夹中的 XML 文件,然后将它们上传到 FTP 站点。
\n\n我添加了一个记录器,它一致地写出以下错误:
\n\n\n\n\nFTP 上传服务启动于 05/08/2016 10:44:42 AM FTP 上传服务\n 错误发生于: 05/08/2016 10:44:42 AM 配置系统无法在\n System.Configuration.ClientConfigurationSystem 初始化。 EnsureInit(String\n configKey) 位于\n System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String\nsectionName) 位于\n System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String\nsectionName) 位于\n System.Configuration.ConfigurationManager.get_AppSettings() at\n FTPUploadService.Service1.ScheduleService() in\n C:\\user\\Desktop\\FTPUploadService\\FTPUploadService\\FTPUploadService\\Service1.cs:line\n 51
\n
我转到代码中的这一特定行(第 51 行),即:
\n\nstring mode = ConfigurationManager.AppSettings["Mode"].ToUpper();\nRun Code Online (Sandbox Code Playgroud)\n\n这里有更多的代码块供参考:
\n\ntry\n {\n Scheduler = new Timer(new TimerCallback(SchedulerCallback));\n string mode = ConfigurationManager.AppSettings["Mode"].ToUpper();\n this.WriteToFile("FTP Upload Service Mode: " + mode + " {0}");\n\n //Set the Default Time.\n DateTime scheduledTime = DateTime.MinValue;\n\n if (mode == "DAILY")\n {\n //Get the Scheduled Time from AppSettings.\n scheduledTime = DateTime.Parse(System.Configuration.ConfigurationManager.AppSettings["ScheduledTime"]);\n if (DateTime.Now > scheduledTime)\n {\n //If Scheduled Time is passed set Schedule for the next day.\n scheduledTime = scheduledTime.AddDays(1);\n }\n }\n\n if (mode.ToUpper() == "INTERVAL")\n {\n //Get the Interval in Minutes from AppSettings.\n int intervalMinutes = Convert.ToInt32(ConfigurationManager.AppSettings["IntervalMinutes"]);\n\n //Set the Scheduled Time by adding the Interval to Current Time.\n scheduledTime = DateTime.Now.AddMinutes(intervalMinutes);\n if (DateTime.Now > scheduledTime)\n {\n //If Scheduled Time is passed set Schedule for the next Interval.\n scheduledTime = scheduledTime.AddMinutes(intervalMinutes);\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n\n我\xe2\x80\x99已经尝试了很多事情,更改app.config的XML格式,确保所需的命名空间在那里,检查app.config的XML语法,以确保它是正确\xe2\x80\xa6none 没有帮助。\n这是 app.config:
\n\nstring mode = ConfigurationManager.AppSettings["Mode"].ToUpper();\nRun Code Online (Sandbox Code Playgroud)\n\n有什么建议么??配置管理器有问题吗?
\n小智 6
我以前遇到过这个问题,很可能是因为 app.config 中的 xml 设置不正确,我相信“configSections”节点必须是“configuration”之后的第一个子节点。查看下面我的 xml 并尝试一下。
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>
</configSections>
<appSettings>
<add key="Mode" value="Interval"/>
<!-- <add key ="Mode" value ="Interval"/>-->
<add key="IntervalMinutes" value="1"/>
<add key="ScheduledTime" value="18:41"/>
<add key="sourceFiles" value="C:\XML Test\"/>
<add key="sourcePath" value="C:\XML Test\"/>
<add key="files" value="*.xml"/>
<add key="allDirectories" value="True"/>
<add key="addDateTimeBeforeUpload" value="True"/>
<add key="hostName" value="XXXXXXXXX"/>
<add key="Port" value="22"/>
<add key="UserName" value="XXXXXXX"/>
<add key="Password" value="XXXXXXXX"/>
<add key="SshHostKeyFingerprint" value=""/>
<add key="uploadpath" value="/XXXXXXX/"/>
<add key="removeSourceFiles" value="false"/>
<add key="SshPrivateKeyPath" value=""/>
<add key="protocol" value=""/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
Run Code Online (Sandbox Code Playgroud)