Reputation: 11
public interface IAuthenticationService
{
Task<TurnaResponse> RegisterUser(User user);
Task<TurnaResponse> LoginUser(string email, string password);
Task<TurnaResponse> ResetPassword(string userId, string password);
Task<TurnaResponse> ForgotPassword(string email);
Task<TurnaResponse> ProcessSession(string userId);
bool IsUserAuthenticated();
}
public async Task<TurnaResponse> ProcessSession(string userId)
{
UriBuilder builder = new UriBuilder(ApiConstants.BaseApiUrl)
{
Path = ApiConstants.ProcessSessionEndpoint
};
var query = System.Web.HttpUtility.ParseQueryString(builder.Query);
query["userId"] = userId;
builder.Query = query.ToString();
var result = await _genericRepository.GetAsync<TurnaResponse>(builder.ToString(), "");
result.Data = result.Data != null
? JsonConvert.DeserializeObject<User>(result.Data.ToString()) : result.Data;
_sessionResponse = (TurnaResponse)result;
return result;
}
How can I call the ProcessSession method below to get the response object in IsAuthenticated method that returns boolean ?
public bool IsUserAuthenticated()
{
var result = !string.IsNullOrEmpty(_settingsService.UserIdSetting);
if (result)
{
ProcessSession(_settingsService.UserIdSetting);
result = _sessionResponse.Status;
}
return result;
}
Upvotes: 1
Views: 171
Reputation: 43429
I would avoid reusing the non-descriptive bool
variable result
to mean different things in different contexts.
public async Task<bool> IsUserAuthenticatedAsync()
{
if (String.IsNullOrEmpty(_settingsService.UserIdSetting)) return false;
var sessionResponse = await ProcessSessionAsync(_settingsService.UserIdSetting);
return sessionResponse.Status; // Assuming Status is a boolean property
}
I added the Async
suffix to comply with the guidelines. I also removed the underscore prefix from the local variable _sessionResponse
for similar reasons.
Upvotes: 0
Reputation: 1
use following code
public async Task<bool> IsUserAuthenticated()
{
var userId= _settingsService.UserIdSetting;
if (!string.IsNullOrEmpty(userId))
{
var _sessionResponse = await ProcessSession(userId);
result = _sessionResponse.Status;
}
return result;
}
Upvotes: -1
Reputation: 421
If you really want to call the asynchronous method from the synchronous method you can have a look at this thread How to call asynchronous method from synchronous method in C#? . There is much explanation about the topic.
Upvotes: 1
Reputation: 89082
use async/await
public async Task<bool> IsUserAuthenticated()
{
var result = !string.IsNullOrEmpty(_settingsService.UserIdSetting);
if (result)
{
var _sessionResponse = await ProcessSession(_settingsService.UserIdSetting);
result = _sessionResponse.Status;
}
return result;
}
Upvotes: 5