I am new to the async await method in C#.
I have a function called InitServiceAsync I am new to the async await method in C# which will init the service and get the tokens for this client
because AcquireTokenForClient(s).ExecuteAsync()
is an async funtion so I use await to wait for authentication data
Then I assign the task.Result
return from InitServiceAsync()
to _service
Will it cause dead lock? Can anyone suggest what is the right way to call AcquireTokenForClient
with async/await correctly?
Thanks
void fun(){
Task<ExchangeService> task = InitServiceAsync();
_service = task.Result;
}
private static async Task<ExchangeService> InitServiceAsync()
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1, tzi);
var cca = ConfidentialClientApplicationBuilder
.Create(appId)
.WithClientSecret(clientSecret)
.WithTenantId(tenantId)
.Build();
var s = new string[] { "https://outlook.office365.com/.default" };
var authResult = await cca.AcquireTokenForClient(s).ExecuteAsync().ConfigureAwait(false);
service.Credentials = new OAuthCredentials(authResult.AccessToken);
return service;
}