モデル タイプを Windows Azure モバイル サービスで動作させるのに苦労しています。次のメンバーを追加する場合を除いて、正常に動作します。
[DataMemberJsonConverter(ConverterType = typeof(DictionaryJsonConverter))]
public IDictionary<Tuple<int, int>, BoardSpaceState> pieceLocations { get; set; }
/**
* All of this serialization could probably be done better,
* but I've spent enough time trying to make it work already.
*/
public class DictionaryJsonConverter : IDataMemberJsonConverter
{
public static Tuple<int, int> tupleOfString(string str)
{
var match = Regex.Match(str, @"\((\d+), (\d+)\)");
// need to grab indexes 1 and 2 because 0 is the entire match
return Tuple.Create(int.Parse(match.Groups[1].Value), int.Parse(match.Groups[2].Value));
}
public object ConvertFromJson(IJsonValue val)
{
var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(val.GetString());
var deserialized = new Dictionary<Tuple<int, int>, BoardSpaceState>();
foreach (var pieceLoc in dict)
{
deserialized[tupleOfString(pieceLoc.Key)] = (BoardSpaceState) Enum.Parse(typeof(BoardSpaceState), pieceLoc.Value);
}
return deserialized;
}
public IJsonValue ConvertToJson(object instance)
{
var dict = (IDictionary<Tuple<int, int>, BoardSpaceState>)instance;
IDictionary<Tuple<int, int>, string> toSerialize = new Dictionary<Tuple<int, int>, string>();
foreach (var pieceLoc in dict)
{
/** There may be an easier way to convert the enums to strings
* http://stackoverflow.com/questions/2441290/json-serialization-of-c-sharp-enum-as-string
* By default, Json.NET just converts the enum to its numeric value, which is not helpful.
* There could also be a way to do these dictionary conversions in a more functional way.
*/
toSerialize[pieceLoc.Key] = pieceLoc.Value.ToString();
}
var serialized = JsonConvert.SerializeObject(toSerialize);
return JsonValue.CreateStringValue(serialized);
}
}
BoardSpaceState.cs :
public enum BoardSpaceState
{
FriendlyPieceShort,
FriendlyPieceTall,
OpponentPieceShort,
OpponentPieceTall,
None
}
これは Azure に問題なく保持され、管理ポータルでデータを確認できます。ただし、でデータをロードしようとするとtoListAsync()
、次の例外が発生します。
{"Object must implement IConvertible."} System.Exception {System.InvalidCastException}
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)\r\n
at Microsoft.WindowsAzure.MobileServices.TypeExtensions.ChangeType(Object value, Type desiredType)\r\n
at Microsoft.WindowsAzure.MobileServices.MobileServiceTableSerializer.Deserialize(IJsonValue value, Object instance, Boolean ignoreCustomSerialization)\r\n
at Microsoft.WindowsAzure.MobileServices.MobileServiceTableSerializer.Deserialize(IJsonValue value, Object instance)\r\n
at Microsoft.WindowsAzure.MobileServices.MobileServiceTableSerializer.Deserialize[T](IJsonValue value)\r\n
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\r\n
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n
at Microsoft.WindowsAzure.MobileServices.TotalCountList`1..ctor(IEnumerable`1 sequence)\r\n
at Microsoft.WindowsAzure.MobileServices.MobileServiceTable`1.<ToListAsync>d__3f.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n
at chivalry.DataManager.<withServerData>d__2.MoveNext() in c:\\Users\\Rosarch\\Documents\\Visual Studio 2012\\Projects\\chivalry\\chivalry\\DataManager.cs:line 35" string
HRESULT は -2147467262 です。
私は何を間違っていますか?
アップデート:
エラーが発生する行は次のとおりです。
private IMobileServiceTable<Game> gameTable = App.MobileService.GetTable<Game>();
// ...
var games = await gameTable.ToListAsync(); // error here
new Dictionary<Tuple<int, int>, BoardSpaceState>()
価値があるのは、から戻った場合も同じエラーが発生することDictionaryJsonConverter.ConvertFromJson
です。