0

I am using a library to access the API of a cloud service called ShareFile. They have a .NET library of methods available, from which I am trying to use the authenticate method called Password Authentication, in the documentation. My implementation looks like this:

public async Task Authenticate(string username, string password)
{
    try
    {
        Debug.WriteLine("Retrieving OAuthService...");
        OAuthService service = new OAuthService(client, clientId, clientSecret);

        Debug.WriteLine("Retrieving OAuthToken...");
        OAuthToken token = await service.PasswordGrantAsync(username, password,
            subdomain, applicationControlPlane); // This line seems to fail

        Debug.WriteLine("Adding credentials...");
        client.AddOAuthCredentials(token);

        Debug.WriteLine("Adding base URI");
        client.BaseUri = token.GetUri();

        Debug.WriteLine("Authentication complete...");
    }
    catch (Exception ex)
    {
        throw new Exception("Unable to authenticate", ex);
    }
}

The remaining variables, are fields in the class. My problem however is this: The code simply aborts, at the line OAuthToken token = await (...), with no error what-so-ever. Have I done something wrong in the implementation of the method/task, or is it simply the library which is poorly implemented?

Jakob Busk Sørensen
  • 5,599
  • 7
  • 44
  • 96
  • What *exactly* do you mean by "The code simply aborts"? What sort of project are you running this from, and what are you observing? – Jon Skeet Nov 17 '17 at 10:26
  • It just seems to end the function. The next line is never reached (i tried adding a break point) and neither is the `catch` section. – Jakob Busk Sørensen Nov 17 '17 at 10:29
  • 3
    What you are experiencing is known as a *deadlock*. Google "c# async await deadlock", you will find a lot of information. – Igor Nov 17 '17 at 10:31
  • 1
    "It just seems to end the function" - does it? Or does it actually *never reach the end of the method*. Does the task never complete? If so, it does indeed sound like you've got a deadock. – Jon Skeet Nov 17 '17 at 10:32
  • @Noceo you are most likely making a blocking call `.Result` or `.Wait()` somewhere higher up the call stack that is causing a deadlock. Start by looking for those blocking calls. – Nkosi Nov 17 '17 at 10:33
  • @Noceo isolate your code and test only the method you posted here. Either create a unit test that calls *this* method only, or copy the code into a separate console application. `await` isn't broken. – Panagiotis Kanavos Nov 17 '17 at 10:38
  • Thank you for you input. It was caused by the wait the method was called, and not the method itself (as suggested). I changed to call to use `Task.Run()` as suggested in the duplicate question. – Jakob Busk Sørensen Nov 17 '17 at 10:43

0 Answers0