I'm using the Graph API to add a batch of users in C# with following code:
_ActiveDirectoryClient.Users.AddUserAsync(newUser)
(newUser is my IUser instance and _ActiveDirectoryClient is my ActiveDirectoryClient instance).
Each user I'm adding is added to the Context of ActiveDirectoryClient with status "Added" and immediatly flushed to the REST service, with in turns update the IUser instance and set its status to "Unchanged". This is perfectly OK for me.
The problem arise when an addition fails for some reason (reason does not matter, this is a good reason, could be incorrect UPN, missing mailnickname, ...). The failed object stays in the Context with status "Added". This shouldn't be a big problem except that next user (succesfull) addition will induce the addition of both the failing and the new user, and this will raison an exception again since the failling user is still failling. The new user is however correctly inserted.
This is really not convenient and I would like to know if there is any solution to clear the context of any object (for instance when I have an exception, I would trap it - just like a does today - remove with the failing object and continue with next ones.
The only solution I've found for now is to create a new client for each user so that context is recreated each time but I don't think this is the correct way to proceed. I could also add more checks before calling the Graph API but for sure there will always be some constraints missing in my checks, and I don't like to duplicate Microsoft checks in my code ... code duplication is always bad even when some of it is not yours.
Thanks in advance