In Blazor Server I'm calling a web service and storing the results in a dynamic object. I want to either convert that dynamic object to a Dictionary object or pass it through to a method so that I can write it to a file. But whatever I try to do with the object, other than to converting it to a String, I get a Runtime error:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Message=The best overloaded method match for 'System.Text.Json.JsonSerializer.Deserialize<System.Collections.Generic.Dictionary<string,object>>(System.Text.Json.JsonDocument, System.Text.Json.JsonSerializerOptions)' has some invalid arguments
My code works fine in a console application, but when I try and run it in Blazor server I get the Runtime Error.
Here is my code:
namespace BlazorWebAssembly.Server.Endpoints
{
public static class CybsAuthEndpoint
{
static SaveAuthData saveAuthData = new SaveAuthData();
private static string Id = string.Empty;
public static async void MapCybsCall(this WebApplication app)
{
ICallForCybsAuth cybsAuth = new CallForCybsAuth();
dynamic jsonObject = new ExpandoObject();
jsonObject = cybsAuth.RunAsyncJson();
var options = new JsonSerializerOptions { WriteIndented = true, Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
var jsonString = JsonSerializer.Serialize(jsonObject, options);
Console.WriteLine(jsonString);
//**** This is what throws the Runtime Error:
var dict = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string>>(jsonObject);
//****
app.MapGet("/cybersource", () => Results.Ok(jsonObject));
}
}
}