配置部分“appSettings”有一个意外的声明

问题描述 投票:0回答:4

我在 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>
c# app-config
4个回答
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>

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();
    }
}

0
投票

我们从未抽出时间来解决这个问题,但从我们的监控工具来看,这表明我们的服务作业超载,导致了 Jon Mitchell 提到的线程问题。

我们通过启动另一个服务实例来分配工作负载来解决这个问题。也许这会帮助其他面临类似问题的人。


0
投票

使用“RefreshSection”时必须小心。在最需要的时候使用RefreshSection,避免频繁刷新。有一个单独的迭代线程,每 10 分钟刷新一次 AppSettings 左右。

This error is due to a conflict between 

ConfigurationManager.RefreshSection("appSettings");

and 

var Key1 = ConfigurationManager.AppSettings["Key1"]
© www.soinside.com 2019 - 2024. All rights reserved.