【问题标题】:DataContractSerializer - Configuration system failed to initialize -- app config settingDataContractSerializer - 配置系统初始化失败——应用配置设置
【发布时间】:2012-10-05 19:36:57
【问题描述】:

我使用 DataContractSerializer 作为一个独立的部分来序列化一些使用 DataContract/Member 的对象......不幸的是,如果我将自定义 App.Config 部分添加到我的解决方案的配置设置中,它会不断抛出异常与此过程无关...例如,我的配置设置如下:

<configuration>
    <appSettings>
         <!--stuff goes here -->
    </appSettings>

    <MyCustomSectionItDoesntLike>
        <!--stuff goes here -->
    </MyCustomSectionItDoesntLike>
</configuration>

然后我拿这个对象并尝试使用内存流写入...

DataContractSerializer serializer = new DataContractSerializer(item.GetType());
using (MemoryStream memoryStream = new MemoryStream())
    {
           serializer.WriteObject(memoryStream, item);

    }

如果我从配置设置中删除 MyCustomSectionItDoesntLike,它可以正常工作,但是当我放回它时,会触发异常:

消息:

类型初始化器 'System.Runtime.Serialization.DiagnosticUtility' 抛出异常。

内部:

无法识别的配置节 MyCustomSectionItDoesntLike。 (D:\test\bin\x86\Debug\test.vshost.exe.config 第 47 行)

我不确定为什么它不关心项目中任何地方的设置是什么,除非我要序列化...是否需要添加设置或配置部分才能使其正常工作?

谢谢!

更新

total bonehead error....与序列化程序无关...

<configuration>
    <configSections>
         <!--This is where i blew it -->
         <section name="MyCustomSectionItDoesntLike" type="System.Stuff.Stuff" />
    </configSections>
    <appSettings>
         <!--stuff goes here -->
    </appSettings>

    <MyCustomSectionItDoesntLike>
        <!--stuff goes here -->
    </MyCustomSectionItDoesntLike>
</configuration>

【问题讨论】:

    标签: c# .net serialization datacontractserializer datacontract


    【解决方案1】:

    您必须在 app.config 文件的 configSections 中引用实现自定义部分的类。

    查看http://msdn.microsoft.com/en-us/library/2tw134k3%28v=vs.100%29.aspx了解更多信息。

    【讨论】:

    • 不确定你的意思,这部分代码没有使用那个配置部分...项目的另一部分使用它并且可以正常访问它...
    • 但是这部分是在configSections中声明的吗?
    • 我完全错过了你指出的“configSections”部分......
      跨度>
    猜你喜欢
    • 2016-12-25
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 2012-04-23
    相关资源
    最近更新 更多