I can call this task. I can't seem to understand what is returned. Much less use it. I just want a True or False so I can save a Role or not.
public ActionResult Create([FromServices]IServiceProvider serviceProvider,string roleName)
{
ModelState.Clear();
try
{
var doesit= DoesRoleExist(serviceProvider, roleName);
if (string.IsNullOrEmpty(roleName))
{
throw new Exception("Invalid Role Name: Cannot be empty and must be unique");
}
context.Roles.Add(new Microsoft.AspNet.Identity.EntityFramework.IdentityRole()
{
Name = roleName,
NormalizedName = roleName.ToUpper()
});
context.SaveChanges();
return RedirectToAction("Index");
}
catch
{
ModelState.AddModelError(string.Empty, string.Format("{0}", "Unable to Create Role. "));
return View();
}
}
I get this when I break on "var doesit=DoesRoleExist(serviceProvider,roleName)"
? doesit
Id = 2093, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}" AsyncState: null CancellationPending: false CreationOptions: None Exception: null Id: 2093 Result: false Status: WaitingForActivation
Here is the Task:
private async Task<bool> DoesRoleExist(IServiceProvider serviceProvider, string roleName)
{
bool result =false;
var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
result = await RoleManager.RoleExistsAsync(roleName);
return result;
}
It's funny but besides the "can't compare boolean using ===" which I ran into when I put an If statement in the ActionResult, I also see that in the above Task, "return result;" runs AFTER "result=await RoleManager.RoleExistsAsync(roleName);". Go figure. Just a simple true or false is all I ask and the ability to compare a boolean to see if I want to add the Role or not.
I want to add that my context.Roles does not have "RoleExists" method. However, for some reason in an async task I can call ".RoleExistsAsync" for some reason.