Skip to content

Problem: SendQueryAsync no response #332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
masouri opened this issue Mar 29, 2021 · 6 comments
Open

Problem: SendQueryAsync no response #332

masouri opened this issue Mar 29, 2021 · 6 comments

Comments

@masouri
Copy link

masouri commented Mar 29, 2021

Problem

As the title suggestest i send a graphql request, i do this in 3 different situations but in one of those
the method SendQueryAsync just stops without any errors(tried try catch and breakpoints).

####Details
I have one Class to do this request, i use it mutliple times, but in one case it doesn't work and i have no idea
why, or how to debbug it.

There is nothing noticable in the Stacktrace aswell as far as im concerned

###This is my Code

using GermanFishing.Model.GraphQLImages;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.Newtonsoft;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace GermanFishing.ViewModel
{
    public  class UserViewModel
    {

        public User user_profile { get; set; }

        public UserViewModel()
        {

            user_profile = new User();

        }


        //Consume User Data from Graphql
        public async Task GetProfileData()
        {
            var client = new GraphQLHttpClient(new GraphQLHttpClientOptions
            {
                EndPoint = new Uri("https://www.german-fishing.de/graphql"),

            }, new NewtonsoftJsonSerializer());


            client.HttpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + Convert.ToString(Application.Current.Properties["Login"]));

            var request = new GraphQLHttpRequest
            {
                Query = @"
                           query {
                                  me {
                                    name
                                    unreadNotifications {
                                      data
                                    }
                                    abonnements {
                                      id
                                      place {
                                        id
                                        name
                                      }
                                    }
                                  }
                                }"
            };

            var response = await client.SendQueryAsync<UserImage>(request).ConfigureAwait(false);

            this.user_profile = response.Data.me;
            


        }

    }
}

Any suggestions how to approach this ?

@sungam3r
Copy link
Member

Can you turn on / check logs on the server side?

@masouri
Copy link
Author

masouri commented Mar 30, 2021

Can you turn on / check logs on the server side?

This is where it gets wacky, server gets a post request returns a 200 code, nothing weird or irregular happening there

@masouri
Copy link
Author

masouri commented Mar 30, 2021

###Additional Information
it could be that im in an Async Deadlock
here is the Code where i acctually call it

using GermanFishing.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace GermanFishing.ViewModel.Authentication
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Authentification 
    {
        public Authentification()
        { 
                
        }
        public bool Auth()
        {

            if (IsLoggedIn() && KeyActive())
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        private bool IsLoggedIn()
        {
            bool is_logged_in = Application.Current.Properties.ContainsKey("IsLoggedIn") ? Convert.ToBoolean(Application.Current.Properties["IsLoggedIn"]) : false;

            return is_logged_in;
        }
        private bool KeyActive()
        {
            UserViewModel user_view_model = new UserViewModel();

            Task task = user_view_model.GetProfileData();

            task.Wait();

            if (user_view_model.user_profile == null)
            {
                return false;
            }
            return true;

        }
      
    }

    
}

@ognjensuvakov
Copy link

Same thing here, after calling SendQueryAsync synchronously with ".Result" on Task nothing happens, no logs no errors.

@ds1709
Copy link

ds1709 commented Apr 10, 2021

I can PR fix of this.

@ds1709
Copy link

ds1709 commented Apr 12, 2021

@masouri assumes one of your GetProfileData calls (which is deadlocked) is from contextual thread (GUI or HttpContext), and you call it like GetProfileData().Result. If so, you're in same problem with @ognjensuvakov. I got same problem by calling SendQueryAsync<...>(...).Result from GUI thread (WinForms).
As a temporary workaround, you can try to wrap SendQueryAsync into Task.Run call like this:

var response = Task.Run(async () => await client.SendQueryAsync<UserImage>(request)).Result;

It will call SendQueryAsync on thread pool and will not capture the synchronization context, so you can avoid deadlock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants