2017-06-29 2 views
1
私は、次のスニペットは、.NETコアで書かれてい

例外ながらwtih DataContractSerializer .NETのコアで

System.Runtime.Serialization.SerializationException:エラー

using System; 
using System.Runtime.Serialization; 
using System.Text; 
using System.Xml; 

namespace BugTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Hello World!"); 

      var xml = @"<PingDataConfirmation xmlns=""http://mycompany.com/MyProduct/Operations""> 
     <SourceGuid>88c02aba-f78c-4749-8665-0caddee08b8a</SourceGuid> 
     <Guid>88c02aba-f78c-4749-8665-0caddee08b8a</Guid> 
     <CreationTime>0001-01-01T00:00:00</CreationTime> 
     </PingDataConfirmation>"; 

      const int buffSize = 1000000; 

      int bytes = 0; 
      int chars = 0; 
      byte[] result = new byte[buffSize]; 
      bool compl; 
      Encoding.UTF8.GetEncoder().Convert(xml.ToCharArray(), 0, xml.Length, result, 0, buffSize, true, out chars, out bytes, out compl); 

      var xmlReader = XmlDictionaryReader.CreateTextReader(result, 0, bytes, new XmlDictionaryReaderQuotas()); 
      var sr = new DataContractSerializer(typeof(PingDataConfirmation)); 
      var deserialized = sr.ReadObject(xmlReader); 
     } 
    } 

    [DataContract(Namespace = "http://mycompany.com/MyProduct/Operations")] 
    public class PingDataConfirmation 
    { 
     [DataMember(IsRequired = true)] 
     public string SourceGuid { get; set; } 

     [DataMember(IsRequired = true)] 
     public string Guid { get; set; } 

     [DataMember(IsRequired = true)] 
     public DateTime CreationTime { get; set; } 
    } 
} 

それが例外をスロー1行目 ポジション86。 'Element' 'SourceGuid' from namespace 'http://mycompany.com/MyProduct/Operations'は期待されていません。期待する 要素 'CreationTime'

したがって、DataContractSerializedのいずれかのプロパティが欠落しているようです。

それで、DataContractSerializerの.netコア実装ではバグかもしれませんか?それが起こるのを見るためにインテリアをデバッグする可能性はありますか?

CSPROJ:

<Project Sdk="Microsoft.NET.Sdk"> 
    <PropertyGroup> 
    <OutputType>Exe</OutputType> 
    <TargetFramework>netcoreapp1.1</TargetFramework> 
    </PropertyGroup> 
    <ItemGroup> 
    <PackageReference Include="System.ServiceModel.Primitives" Version="*" /> 
    <PackageReference Include="System.ServiceModel.Http" Version="*" /> 
    </ItemGroup> 
</Project> 

.NETのコア:

1.0.4 

答えて

1

あなたの問題は、あなたのdata member orderは、XMLの要素の順序と矛盾しているということです。 Data Member Orderから

  • データコントラクト型が継承階層の一部である場合は、その基本型のデータメンバーは、常に最初の順序です。

  • 次に、 のアルファベット順に、DataMemberAttribute属性セットのOrderプロパティを持たない現在の型のデータメンバを示します。

  • 次に、DataMemberAttribute属性セットのOrderプロパティを持つデータメンバを示します。これらの値は、最初に の値、次にOrderの値の順で並べ替えられ、さらに がある場合はアルファベット順に並べられ、特定のOrder値の1つのメンバーよりも大きくなります。注文額は をスキップした可能性があります。

そしてData Contract Equivalence: Data Member Order and Data Contract equivalenceから:

は、データメンバーの順序が一致して 基本型のメンバーが順番に派生型のメンバーの前にいることをしなければならないことに留意してください。したがって

XMLの要素の順序はアルファベット順である必要があり、またはDataMemberAttribute.Order値はXMLの要素の実際の順序と一致していることを指定する必要があり、次のいずれか

[DataContract(Namespace = "http://mycompany.com/MyProduct/Operations")] 
public class PingDataConfirmation 
{ 
    [DataMember(IsRequired = true, Order = 1)] 
    public string SourceGuid { get; set; } 

    [DataMember(IsRequired = true, Order = 2)] 
    public string Guid { get; set; } 

    [DataMember(IsRequired = true, Order = 3)] 
    public DateTime CreationTime { get; set; } 
} 
関連する問題