I do have Azure function. I've implemented Dependency Injection which works just find in general, except one tricky case.
I'd like to make some validation calls to external service (Microsoft Graph in my case) DURING deserialization.
Now can I use graph client (for example) in custom class used for JSON deserialization?
using Microsoft.Graph;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace LNG.S365
{
public class ProjectItem
{
public readonly GraphServiceClient graph;
private string _DisplayName;
[JsonConstructor]
private ProjectItem(GraphServiceClient graph)
{
this.graph = graph; // here Graph NOESN'T work. Is null
}
[JsonProperty("DisplayName", Required = Required.Always)]
public string DisplayName
{
get => _DisplayName;
set
{
var users = graph.Users.Request().GetAsync();
this._DisplayName = value;
}
}
}
public class Triggers
{
private readonly GraphServiceClient graph;
public Triggers(GraphServiceClient graph)
{
this.graph = graph; // here Graph works
}
[FunctionName("ProjectPost")]
public async Task<HttpResponseMessage> ProjectPost
(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "project")] HttpRequestMessage req,
[DurableClient] IDurableOrchestrationClient starter,
[DurableClient] IDurableEntityClient client,
ILogger log,
Microsoft.Azure.WebJobs.ExecutionContext executionContext
)
{
var users = graph.Users.Request().GetAsync(); // here Graph works
// Here i'd like to deserialize request body and make some validation ON DESERIALIZATION step, not after (outside deserialization)
var requestBody = req.Content.ReadAsStringAsync().Result;
var projectRequestData2 = JsonConvert.DeserializeObject<ProjectItem>(requestBody);
}
}
}