【问题标题】:DataContractSerializer works on Project A but Fails on Project B on Same Machine why?DataContractSerializer 在项目 A 上工作,但在同一台机器上的项目 B 上失败,为什么?
【发布时间】:2017-11-16 12:47:49
【问题描述】:

我有一个调用 DataContractSerializer 的库。我已经在 ConsoleApp 和 Windows Service App 中使用了好几个月了。这些都是在 Visual Studio 2015 中创建的。

本周我创建了一个新的 Win Forms 应用程序(在 Visual Studio 2017 中)并使用该 .dll 代码。相同的代码,相同的类,相同的文件 - 2 个旧项目仍然有效,新项目抛出此错误。

注意:我没有使用 app.config,因此与该主题相关的所有现有帖子都不适用。另外,我的文件中没有 configSections 或 configuation 节点,因此这些答案不适用。

{System.Configuration.ConfigurationErrorsException:配置 系统初始化失败---> System.Configuration.ConfigurationErrorsException:只有一个 每个配置文件允许的元素,如果存在必须 成为根元素的第一个子元素。

机器配置是相同的,因为它们都在我的机器上运行。所以,我不知道还有什么可能。有什么想法吗?

在我现有的 2 个项目中正常工作的文件开头为:

> <SettingsModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
> xmlns="http://schemas.datacontract.org/2004/07/Model">   <SettingsList
> xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
>     <d2p1:KeyValueOfstringSettingsOFv3k_StQ>

适用于前 2 个项目的反序列化代码:

 using (Stream stream = new MemoryStream())
 {
     byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
     stream.Write(data, 0, data.Length);
     stream.Position = 0;
     DataContractSerializer deserializer = new DataContractSerializer(typeof(GatewaySettingsModel));
     return deserializer.ReadObject(stream) as GatewaySettingsModel;
  }

编辑

工作应用的配置文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
  </startup>
  <appSettings>
    <add key="DataOption" value="5" />
  </appSettings>
  <entityFramework>

非工作应用的配置文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
  </startup>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="..." connectionString="..." />
    <add name="..." connectionString="..." providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

根据下面的评论和错误文本,“启动”标签必须移到“配置”标签之后。我不明白为什么只为 DataContract 序列化抛出此错误,但事实就是如此。

【问题讨论】:

  • 请包含每个应用程序的配置文件。
  • @mjwills 你很聪明。

标签: c# datacontractserializer


【解决方案1】:

根据configSections Element (General Settings Schema) 文档

configSections 元素在配置文件中,configSections 元素必须是配置元素的第一个子元素。

由于配置文件是可配置的(您可以添加许多节和自定义节),序列化程序需要能够理解这些配置节,这个要求(即首先)消除了在定义处理程序之前使用节等问题.

【讨论】:

    猜你喜欢
    • 2021-06-16
    • 1970-01-01
    • 2019-12-23
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多