以下は、DataContractSerializerを使用してCarというクラスを直列化して逆シリアル化しようとする非常に短いUWP単体テストのテストです。私は、アプリケーションが中断されたときにセッションの状態を保存するために、このタイプのコードをUWPアプリで使用する予定です。私はKnownTypesコレクションにすべての型を追加する必要がないので、msdnブログから簡単なカスタムDataContractResolverを盗みました。シリアライゼーションとデシリアライゼーションが同じアプリ内で行われるとき(つまり、型とアセンブリを共有するとき)に動作するはずです。完全な.NET Framework 4.6.2でコードが実行されている場合は、完全に機能します。しかし、WEIRD THINGは、「.NETネイティブツールチェーンでコンパイル」を有効にしていない限り、全く同じコードがユニバーサルWindowsプロジェクトの一部であることに失敗するということです。UWP、.NETネイティブの問題でDataContractSerializer + DataContractResolverの問題?
.NETネイティブツールチェーンを使用せずにUWPアプリケーションで全く同じコードが動作しないのはなぜですか? .NET Nativeはシリアライゼーションの問題を引き起こすはずですので、.NET Nativeを使用するとUWPでしか動作しないというのは非常に奇妙です。 .NETを使用しないでUWPアプリケーションで動作させるにはどうしたらいいですか?ネイティブコンパイルは、DEBUGビルドがオンになっているときにDEBUGビルドで劇的に遅くなります。
ここでは、両方のユニットテストを含む完全なソリューションへのGitHubリンクがあります。
ここusing System;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using System.Runtime.Serialization;
using System.Xml;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
namespace ResolverTest {
[TestClass]
public class SerializerTestUniversal {
[TestMethod]
public void CanRoundtripComplexTypeWithNoKnownTypesAndCustomResolver() {
// prepare object for serialization
var car = new Car { Year = 2000, Model = "Ford" };
var rootToSerialize = new Dictionary<string, object> { ["car"] = car };
// serialize with DataContractSerializer and NO known types
// hopefully the custom DataContractResolver will make it work
var serializer = new DataContractSerializer(
typeof(Dictionary<string, object>),
new DataContractSerializerSettings { DataContractResolver = new SharedTypedResolver() });
var memoryStream = new MemoryStream();
serializer.WriteObject(memoryStream, rootToSerialize);
// deserialize
memoryStream.Position = 0;
var output = (Dictionary<string, object>)(serializer.ReadObject(memoryStream));
var outputCar = (Car)output["car"];
// check that the data got roundtripped correctly
Assert.AreEqual(car.Year, outputCar.Year);
Assert.AreEqual(car.Model, outputCar.Model);
}
public class Car {
public int Year { get; set; }
public string Model { get; set; }
}
// To be used when serializing and deserializing on same machine with types defined in a shared assembly
// Intended to used for suspend/resume serialization in UWP apps
// Code from https://blogs.msdn.microsoft.com/youssefm/2009/06/05/configuring-known-types-dynamically-introducing-the-datacontractresolver/
public class SharedTypedResolver : DataContractResolver {
public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver) {
return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null) ?? Type.GetType($"{typeName}, {typeNamespace}");
}
public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace) {
if (!knownTypeResolver.TryResolveType(dataContractType, declaredType, null, out typeName, out typeNamespace)) {
XmlDictionary dictionary = new XmlDictionary();
typeName = dictionary.Add(dataContractType.FullName);
typeNamespace = dictionary.Add(dataContractType.GetTypeInfo().Assembly.FullName);
}
return true;
}
}
}
}
は、.NETネイティブがオンになっているとき、それを動作させるためにUWPに必要なフルrd.xmlファイルの内容である:ここで
https://github.com/jmagaram/CustomResolverは、ユニットテストコードです。
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Application>
<Assembly Name="*Application*" Dynamic="Required All" />
<Type Name="ResolverTest.SerializerTestUniversal.Car" Browse="Required Public" DataContractSerializer="Required All"/>
</Application>
</Directives>
そして最後に、これは、.NETネイティブがオフになっている場合に発生する例外です:私はそれがで動作するように取得することができた
Result Message: Test method ResolverTest.SerializerTestUniversal.CanRoundtripComplexTypeWithNoKnownTypesAndCustomResolver threw exception:
System.Runtime.Serialization.SerializationException: Type 'ResolverTest.SerializerTestUniversal+Car' with data contract name 'SerializerTestUniversal.Car:http://schemas.datacontract.org/2004/07/ResolverTest' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
== UPDATED ==
別のDataContractResolver。以下のコードを参照してください。シリアル化中に新しいリゾルバが状態/情報を構築するため、デシリアライズ用のDataContractSerializerの新しいインスタンスを使用するようにテストを変更しました。このコメントは、UWPと.NET 4.6.2でDataContractResolverがどのように異なって使用されているかを説明しています。 .NET Nativeが有効になっていないと、元のコードが失敗した理由はまだ分かりません。
public class SharedTypeResolver : DataContractResolver {
Type _mostRecentResolvedType = null;
// When an object is serialized using the Universal Windows Platform (as of version
// 5.2.2), the ResolveName method is called for each type it encounters immediately after
// calling TryResolveType. The Microsoft API specification says the ResolveName method is
// used to 'map the specified xsi:type name and namespace to a data contract type during
// deserialization', so it is a bit surprising this method is called during
// serialization. If ResolveName does not return a valid type during serialization,
// serialization fails. This behavior (and the failure) seems to be unique to the UWP.
// ResolveName is not called during serialization on the .Net Framework 4.6.2.
//
// During serialization it is difficult to force ResolveName to return a valid type
// because the typeName and typeNamespace do not include the assembly, and
// Type.GetType(string) can only find a type if it is in the currently executing assembly
// or it if has an assembly-qualified name. Another challenge is that the typeName and
// typeNamespace parameters are formatted differently than Type.FullName, so string
// parsing is necessary. For example, the typeNamespace parameter looks like
// http://schemas.datacontract.org/2004/07/namespace and the typeName parameter is
// formatted as className+nestedClassName. Type.FullName returns a single string like
// namespace.class+nestedClass. But even worse, generic types show up in ResolveName
// during serialization with names like 'StackOfint'. So the HACK approach I've taken
// here is to cache the last Type seen in the TryResolveType method. Whenever a
// typeNamespace appears in ResolveName that does not look like a real assembly name,
// return the cached type.
//
// During deserialization it is very easy for this method to generate a valid type name because the XML
// file that was generated contains the full assembly qualified name.
public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver) {
if (typeNamespace.StartsWith("http://schemas.datacontract.org")) {
// Should only happen on UWP when serializing, since ResolveName is called
// immediately after TryResolveType.
return _mostRecentResolvedType;
}
else {
// Should happen when deserializing and should work with all types serialized
// with thie resolver.
string assemblyQualifiedTypeName = $"{typeName}, {typeNamespace}";
return Type.GetType(assemblyQualifiedTypeName);
}
}
public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace) {
_mostRecentResolvedType = dataContractType;
XmlDictionary dictionary = new XmlDictionary();
typeName = dictionary.Add(dataContractType.FullName);
typeNamespace = dictionary.Add(dataContractType.GetTypeInfo().Assembly.FullName);
return true;
}
}
Thxを:) –