Deserialise string to an interface type without implementation C#

55 Views Asked by At

I am building an azure function to handle a discord bot with the interactions url endpoint. I am receiving the requests perfectly fine but I am struggling with converting the request body to the correct type. This is the code I am trying out:

[Function("InteractionHandler")]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestData req)
{
    _logger.LogInformation("InteractionHandler");


    DefaultContractResolver contractResolver = new DefaultContractResolver
    {
        NamingStrategy = new CamelCaseNamingStrategy()
    };

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    _logger.LogInformation(requestBody);

    // Deserialize the JSON to a dynamic object (JObject)  
    IDiscordInteraction? data = JsonConvert.DeserializeObject<IDiscordInteraction>(requestBody, new JsonSerializerSettings() { ContractResolver = contractResolver });

    //more code here that uses the data...
}

So I know the req.Body will always follow the IDiscordInteraction interface as per documentation, so how do I use this code without getting following error: Exception: System.AggregateException: One or more errors occurred. (Could not create an instance of type Discord.IDiscordInteraction. Type is an interface or abstract class and cannot be instantiated. Path 'app_permissions', line 1, position 19.)

enter image description here

0

There are 0 best solutions below