question

bebr avatar image
bebr asked

LoginWithCustomIDAsync does not complete

Hello,

I am trying to follow the Getting Started tutorial and am stuck on the Make an API Call part. I am trying to log in, but it does not complete. I have seen the posts on this Playfab question but those did not work for me. I am using the C# SDK and have tried the following:

bool _running = true;

PlayFabSettings.TitleId = "1234"; // using my actual ID in place of the 1234

var request = new LoginWithCustomIDRequest { CustomId = "11111111111111", CreateAccount = true }; // used my CustomID here in place of the 1111111111111

var loginTask = PlayFabClientAPI.LoginWithCustomIDAsync(request);

loginTask.ContinueWith((result) => { _running = false; }); 

while (_running) {  
    Console.WriteLine("Waiting for result...");
}

I have also tried the exact code (copy/paste) from the CSharp Getting Start documentation on how to do this, and that does not work either.

When I run my code using either of these, it just sits and does not finish. When I check the SDK, it appears to be getting stuck at this line in PlayFabClientAPI.cs, method LoginWithCustomIDAsync:

var httpResult = await PlayFabHttp.DoPost("/Client/LoginWithCustomID", request, null, null, extraHeaders);

I am not sure how to proceed. I have already read that it could be a "deadlocking" issue, though I am unsure where that could be occurring or how to resolve given that I've tried everything I mentioned above. Please advise.

,
apis
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1807605288 avatar image
1807605288 answered

I have found that many of the times that CSharp "doesn't finish", the async task actually threw an exception, and it was not caught or reported properly.

Add a check to your "while(_running) to check loginTask.Exception == null"

I need to update the examples to check for this a little more carefully as well.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

bebr avatar image
bebr answered

@Paul Gilmore Thanks for your reply. I did not have the chance to give this a try, because I managed to make it work using the below code snippet just moments before you replied.

My understanding is that because the login is async, the confusion comes when trying to await its completion in a non-async method (such as in the code in my original post). By doing the login on an async thread (created by Task.Run(async() ) instead of in a sync method (waiting for the loginTask in my original post), I can await the completion properly (await PlayFabClientAPI.LoginWithCustomIDAsync(request)).

var login = Task.Run(async () =>
{
    PlayFabSettings.TitleId = "1234";
    var request = new LoginWithCustomIDRequest { CustomId = "1111111111111", CreateAccount = false };
    var loginTask = await PlayFabClientAPI.LoginWithCustomIDAsync(request);
    Console.WriteLine("LOGIN");
    Console.WriteLine($"Session ticket: {loginTask.Result.SessionTicket}");
 
});
login.Wait();
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1807605288 avatar image 1807605288 ♦ commented ·

Ahh yes. When you can await the result, that typically works much better. The console app example I built doesn't cooperate with await very well.

I hope to improve that example at some point.

There's also a nuget package for CSharp SDK, which is not yet properly described in the Getting Started Guide.

https://www.nuget.org/packages/PlayFabAllSDK/

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.