Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9203523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:29:32+00:00 2026-06-17T23:29:32+00:00

I’m struggling trying to get my model type to work with Windows Azure Mobile

  • 0

I’m struggling trying to get my model type to work with Windows Azure Mobile Services. It works fine, except when I add the following member:

    [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
    }

This persists to Azure just fine, and I can see the data in the management portal. However, when I try to load the data with toListAsync(), I get the following exception:

{"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

The HRESULT is -2147467262.

What am I doing wrong?

Update:

The line I get the error is:

private IMobileServiceTable<Game> gameTable = App.MobileService.GetTable<Game>();
// ...
var games = await gameTable.ToListAsync();  // error here

For what it’s worth, I also get the same error if I just return new Dictionary<Tuple<int, int>, BoardSpaceState>() from DictionaryJsonConverter.ConvertFromJson.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:29:33+00:00Added an answer on June 17, 2026 at 11:29 pm

    That seems to be a bug in the Azure Mobile Services client SDK – I’ll file it with the product team, thank you for reporting it.

    Meanwhile, there is a workaround which will get it working: instead of declaring the pieceLocations variable with the interface type, if you declare it with the dictionary type then it will work – here’s the code which I just tried.

    public sealed partial class MainPage : Page
    {
        public static MobileServiceClient MobileService = new MobileServiceClient(
            "https://YOUR-APP-NAME-HERE.azure-mobile.net/",
            "YOUR-APP-KEY-HERE"
        );
    
        public MainPage()
        {
            this.InitializeComponent();
        }
    
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
    
        private async void btnStart_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                Game game = new Game();
                game.pieceLocations = new Dictionary<Tuple<int, int>, BoardSpaceState>();
                for (int i = 0; i < 5; i++)
                {
                    for (int j = 0; j < 5; j++)
                    {
                        game.pieceLocations[Tuple.Create(i, j)] = BoardSpaceState.None;
                    }
                }
    
                game.pieceLocations[Tuple.Create(1, 2)] = BoardSpaceState.FriendlyPieceShort;
                game.pieceLocations[Tuple.Create(2, 1)] = BoardSpaceState.FriendlyPieceTall;
                game.pieceLocations[Tuple.Create(3, 4)] = BoardSpaceState.OpponentPieceShort;
                game.pieceLocations[Tuple.Create(4, 3)] = BoardSpaceState.OpponentPieceTall;
    
                var table = MobileService.GetTable<Game>();
                await table.InsertAsync(game);
                AddToDebug("Inserted game: ", game.Id);
    
                AddToDebug("Now trying to retrieve it...");
    
                var allGames = await table.ToListAsync();
                AddToDebug("All games, length = {0}", allGames.Count);
            }
            catch (Exception ex)
            {
                AddToDebug("Error: {0}", ex);
            }
        }
    
        void AddToDebug(string text, params object[] args)
        {
            if (args != null && args.Length > 0) text = string.Format(text, args);
            this.txtDebug.Text = this.txtDebug.Text + text + Environment.NewLine;
        }
    }
    
    [DataTable(Name = "Test")]
    public class Game
    {
        public int Id { get; set; }
        [DataMemberJsonConverter(ConverterType = typeof(DictionaryJsonConverter))]
        public Dictionary<Tuple<int, int>, BoardSpaceState> pieceLocations { get; set; }
    }
    
    public enum BoardSpaceState
    {
        FriendlyPieceShort,
        FriendlyPieceTall,
        OpponentPieceShort,
        OpponentPieceTall,
        None
    }
    
    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.