I have a synchronous method:
public void DoStuff() {
DoThings();
GraphClient.SetExtendedProperty(user, propertyName, value); // this method occasionally throws an exception
DoOtherThings();
}
Call3rdPartyMethod
makes a REST API call using the Azure Ad Graph Client API which throws an exception if I try to set the value of an extended property on Active Directory and it isn't found. This usually happens a new user is added to the directory and the Extension Properties feature hasn't extended the user schema before I want to set the values (it seems to take a few seconds).
I replaced the SetExtendedProperty call with my own wrapper containing the call in a busy-wait loop thus:
public void TrySetProperty(GraphObject user, string propertyName, string value)
{
var exceptions = new List<Exception>();
for (int retry = 0; retry < 5; retry++)
{
try
{
if (retry > 0)
Thread.Sleep(1000);
GraphClient.SetExtendedProperty(user, propertyName, value);
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}
throw new AggregateException(exceptions);
}
}
The issue is that I want to be able to call TrySetProperty
from both a synchronous and asynchronous method:
public void DoStuff() {
DoThings();
TrySetProperty(user, propertyName, value);
DoOtherThings();
}
public Task DoOtherStuffAsync() {
await DoAsyncThings();
TrySetProperty(user, propertyName, value);
await DoOtherAsyncThings();
}
I can't change SetExtendedProperty to be asynchronous, and I'm concerned I shouldn't be using Thread.Sleep if i'm calling it from an asynchronous method - rather Task.Delay(). Can anyone advise?