Reputation: 6227
I've added a bool
method, that provides performs OAuth authentication and returns a true or false. Bases on the result of the user and password check.
But I'm getting an error on LoginService()
method, stating that:
'MongoDBApp.Services.AuthenticationService.LoginService(string, string)': not all code paths return a value
I Googled the error and results show it's becuase as the error says, the method doesn't return the values defined in the method return type.
I've checked the method over and it's returning true or false at each condition.It seems that the bool values aren't being returned outside of the LoginAsync()
inner method.
Does anyone know why the method is not receiving the bool values returned?
This is the LoginService()
with return type bool:
private bool LoginService(string username, string password)
{
string ConnectionName = "Username-Password-Authentication";
auth0.LoginAsync(connection: ConnectionName, userName: username, password: password).ContinueWith(t =>
{
if (t.IsFaulted)
{
return false;
}
else
{
Messenger.Default.Send<UpdateLoginMessage>(new UpdateLoginMessage());
return true;
}
},
TaskScheduler.FromCurrentSynchronizationContext());
}
Upvotes: 1
Views: 2513
Reputation: 97686
There is no return true
or return false
in your LoginService
method, which is what the compiler is complaining about. (You do have return true
and return false
, but they're not in LoginService
, they're in the lambda you're passing to ContinueWith
, and that lambda is a separate method.)
But even worse, you're starting a LoginAsync
call to do its work in a background thread, and then returning from your LoginService
method immediately, before that LoginAsync
-- or the continuation you registered -- have completed. That's almost certainly not what you want.
What you really need is an async
method:
private async Task<bool> LoginServiceAsync(string username, string password)
{
string ConnectionName = "Username-Password-Authentication";
try {
var result = await auth0.LoginAsync(
connection: ConnectionName, userName: username, password: password);
Messenger.Default.Send<UpdateLoginMessage>(new UpdateLoginMessage());
return true;
} catch {
return false;
}
}
(It's also a terrible idea to turn exceptions into Boolean results without at least logging the exception, but I didn't mess with that part of your logic.)
The caller would then say:
var loginResult = await LoginServiceAsync(username, password);
At which point all your code would successfully wait for the async calls to complete.
Upvotes: 4