3

I'm developing a Blazor app where, in the function section of one of my views, I have:

var dict = await Http.GetJsonAsync<Dictionary<Node, Decimal>>($"api/Home/test");

Node is a custom object I've defined. Then in the corresponding controller action, I have:

[HttpGet("[action]")]
public Dictionary<Node, decimal> test()
{
    var dict = new Dictionary<Node, decimal>();
    var node = new Node();
    dict[node] = 10m;

    return dict;
}

When I try to fetch the dictionary in this way, I see this error in the console:

WASM: [System.ArgumentException] The value "App.Shared.Node" is not of type "App.Shared.Node" and cannot be used in this generic collection.

If I new up one of these dictionaries, but put nothing it it, I don't see this error. I suspect that something about the fact of using my Node objects as keys is getting me in trouble, but it's not clear to me why this shouldn't work. (I certainly have no problem using these dictionaries elsewhere in my application.)

user3678429
  • 258
  • 1
  • 3
  • 9
  • Your original wording "WASM: [System.ArgumentException] The value "App.Shared.Node" is not of type "App.Shared. **Graph** .Node" and cannot be used in this generic collection." Made more sense as a error message. It would be related to but dsitricnt from https://stackoverflow.com/a/53294393/3346583 How certain are you that this the actuall message? – Christopher Nov 18 '18 at 05:45
  • Yes, I've tried to simplify things a little to make this post more concise. I can confirm that the value and type cited in the error message read the same, however. – user3678429 Nov 18 '18 at 05:48
  • Can you please display the full definition of the Node class... – enet Nov 18 '18 at 09:27
  • I believe that the problem is related to serializing your dictionary to JSON, and then deserialinizing the JSON data to a dictionary object. First question to ask is can a class instance serves as a key in a dictionary that is to be serialized ? I'm not 100% sure of this. Second question, is the JSON utility class, which is supposed to be changed, can do the task of deserialization. I'd suggest you post your question in github as a bug, and see what the experts have to say – enet Nov 18 '18 at 12:15
  • I think Isaac has it. I found that when serializing the dictionary, any Node object key would be converted to the string "App.Shared.Node" (which explains the weird error message saying that "App.Shared.Node" is not of type "App.Shared.Node"). It looks like the issue is addressed in this question: https://stackoverflow.com/questions/24504245/not-ableto-serialize-dictionary-with-complex-key-using-json-net – user3678429 Nov 18 '18 at 13:12

0 Answers0