我在C#中的app.config
文件遇到问题
大多数时间服务都可以阅读<appsetting>
部分,但有时下面会出错。
此问题是间歇性发生的,因此很难找到错误的原因:
“配置部分'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>
答案 0 :(得分:0)
更新<add />
中的<appSettings />
元素,以解决此问题:
<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>
答案 1 :(得分:0)
我相信这是一个线程问题。我可以使用以下代码重现该问题。
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();
}
}