C# 配置部分';应用程序设置';有一个意外的声明
我在c中的C# 配置部分';应用程序设置';有一个意外的声明,c#,app-config,C#,App Config,我在c中的app.config文件中遇到问题# 大多数时候,服务可以读取部分,但有时下面会出现错误 此问题间歇性出现,因此很难找到错误原因: “配置节“appSettings”包含意外声明。” 我的app.config如下所示: <?xml version="1.0"?> <configuration> <system.runtime.remoting> <application> <channels> <ch
app.config
文件中遇到问题#
大多数时候,服务可以读取部分
,但有时下面会出现错误
此问题间歇性出现,因此很难找到错误原因:
“配置节“appSettings”包含意外声明。”
我的app.config
如下所示:
<?xml version="1.0"?>
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel ref="tcp" port="5555">
<serverProviders>
<formatter ref="binary" />
</serverProviders>
</channel>
</channels>
<service>
<wellknown mode="SingleCall" type="Sample, Sample.BSL" objectUri="Sample.BSL.Common.bin" />
</service>
</application>
<customErrors mode="Off" />
</system.runtime.remoting>
<appSettings>
<add key="KEY_1" value="Sample.Service.exe.config"></add>
<add key="KEY_2" value="VALUE"/>
<add key="KEY_3" value="VALUE"></add>
<add key="KEY_4" value="VALUE"></add>
<add key="KEY_5" value="VALUE"></add>
<add key="KEY_6" value="VALUE"></add>
<add key="KEY_7" value="VALUE"></add>
<add key="KEY_8" value="VALUE"></add>
<add key="KEY_9.ServiceUri" value="" />
<add key="KEY_10" value="D:\VALUE\Log\Sample.Service" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
</configuration>
像这样更新
的
元素以解决问题:
<appSettings>
<add key="KEY_1" value="Sample.Service.exe.config"/>
<add key="KEY_2" value="VALUE"/>
<add key="KEY_3" value="VALUE"/>
<add key="KEY_4" value="VALUE"/>
<add key="KEY_5" value="VALUE"/>
<add key="KEY_6" value="VALUE"/>
<add key="KEY_7" value="VALUE"/>
<add key="KEY_8" value="VALUE"/>
<add key="KEY_9.ServiceUri" value="" />
<add key="KEY_10" value="D:\VALUE\Log\Sample.Service" />
</appSettings>
我认为这是一个线程问题。我可以用下面的代码重现这个问题
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Running test....");
string value = "";
var t1 = Task.Run(() =>
{
for (int i = 0; i < 100_000; i++)
{
value = ConfigurationManager.AppSettings["Test"];
}
});
var t2 = Task.Run(() =>
{
for (int i = 0; i < 1000; i++)
{
ConfigurationManager.RefreshSection("appSettings");
}
});
Task.WaitAll(t1, t2);
Console.WriteLine("DONE");
Console.ReadLine();
}
}
类程序
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“运行测试…”);
字符串值=”;
var t1=Task.Run(()=>
{
对于(int i=0;i<100_000;i++)
{
值=ConfigurationManager.AppSettings[“测试”];
}
});
var t2=Task.Run(()=>
{
对于(int i=0;i<1000;i++)
{
ConfigurationManager.RefreshSection(“应用设置”);
}
});
Task.WaitAll(t1,t2);
控制台。写入线(“完成”);
Console.ReadLine();
}
}
我想知道这是否与
与
有关(对于XML,我认为两者都是正确的,但可能太麻烦了?试试后者)它也缺少了顶部的通常编码=“utf-8”
,我将所有键都改为“”,但还是出现了。你有没有想过如何解决这个问题?谢谢你的建议!但我把所有键都改成了“”,但还是发生了。