1
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;
}
phuzi
  • 12,078
  • 3
  • 26
  • 50
  • You may turn it into async method as suggested in the answer below or call Sync-over-Async (that will freeze the current Thread until the method will return result). `_sessionResponse = ProcessSession(_settingsService.UserIdSetting).GetAwaiter().GetResult();` – aepot May 18 '20 at 17:04
  • I tried this one but after it calls and takes the response from API method it does not bring to the xamarin code. I suppose the async methods cause confusion. – Samet Girgin May 18 '20 at 20:59
  • Maybe it cause a deadlock. Use the answer then. – aepot May 18 '20 at 21:21

4 Answers4

5

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;
}
Jason
  • 86,222
  • 15
  • 131
  • 146
1

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.

Eugene D
  • 347
  • 1
  • 3
  • 17
0

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.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
-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;
}