从 Maui 调用 graphQLClient 时出现死锁

Deadlocks whenever calling graphQLClient from Maui

提问人:user2579571 提问时间:11/18/2023 更新时间:11/18/2023 访问量:24

问:

我真的很难从毛伊岛的 GraphQL 查询中获取任何数据。我在页面的初始值设定项中有代码,并且遇到了死锁。我现在明白了。因此,我已将代码移动到页面上的按钮上。它挂在任务 GetDocumentAsync 下面的行上:

var graphQLResponse = await graphQLHttpClient.SendQueryAsync<RootObject>(documentRequest);

几乎相似的代码在控制台应用中工作正常。但是我无法让它在 Maui 应用程序中返回。没有错误,只是挂起。

以下是一些相关代码:

页面上的按钮:

主页 XAML

<VerticalStackLayout Margin="20">
    <Button Text="Load"
            VerticalOptions="Center"
            HorizontalOptions="Center"
            Clicked="OnButtonClicked"/>
    <Entry x:Name="documentId"
           FontSize="24"
           Keyboard="Chat"
           Placeholder="Enter\Scan Id"
           ClearButtonVisibility="WhileEditing"/>
    <ListView ItemsSource="{Binding ErpDocument}" 
              HasUnevenRows="True" 
              VerticalOptions="FillAndExpand"
              SeparatorColor="Black"
              ItemSelected="OnListItemSelected"
              SelectedItem="{Binding SelectedListItem,Mode=TwoWay}">
        <ListView.BindingContext>
            <viewModels:DocumentViewModel />
        </ListView.BindingContext>
    

代码隐藏

async void OnButtonClicked(object sender, EventArgs args)
{
    DocId key = new DocId();
    key.Id1 = "Default";
    key.Id2 = "WF201B";
    key.Id3 = "000";
    ErpPart erpDocument = ErpPart.Load(key); ;
}
    

DOCUMENTVIEWMODEL 代码

public static ErpPart Load(DocId key)
    {
        string qlQuery = "{ products(filter:{and: [{fac: {eq: \"" + key.Id1 + "\" }}, {fpartno: {eq: \"" + key.Id2 + "\"}}, {frev: {eq: \"" + key.Id3 + "\" }} ]}) " +
                        @"{
                                items {
                                    id1 : fpartno
                                    id2 : frev
                                    id3 : fac
                                    id4 : fac
                                    name : fdescript
                                    OnHand {
                                        locations: items {
                                            location: flocation
                                            bin : fbinno
                                            lotsn : flot
                                            onhand : fonhand
                                        }
                                    }
                                    uofm : fmeasure
                                    source : fsource
                                    purchased : fcpurchase
                                    status : fcstscode
                                }
                            }
                        }";
    Item doc = new ();
    Item[] docs = null;

    try
    {
        var rootObj = new RootObject();
        rootObj = GetDocumentAsync(qlQuery).GetAwaiter().GetResult();
        docs = rootObj.products.items;
    }
    catch (Exception ex)
    {
        Debug.Print(ex.Message);
    }

    if (docs != null)
    {
        foreach (var row in docs)
        {
            doc.Id1 = row.Id1;
            doc.Id2 = row.Id2;
            doc.Id3 = row.Id3;
            doc.Id4 = row.Id4;
            doc.Name = row.Name;
            doc.Type = "Part";
            doc.Onhand = row.Onhand;
            doc.UofM = row.UofM;
            doc.Source = row.Source;
            doc.Purchased = row.Purchased;
            doc.Status = row.Status;
            //doc.Color = MauiProgram.colorPart;

            Location[] inventory = row.Onhand.Locations;
            Debug.Print("");
            decimal onhand = 0;
            if (inventory != null && inventory.Length > 0)
            {

            }
            else
            {
                //Debug.Print("No OnHand Exists.");
            }
        }
    }
    else
    {
        Debug.Print("No matching Parts found in query.");
    }
    return new() { Part = doc };
}

private static async Task<RootObject> GetDocumentAsync(string iQuery)
{

    var graphQLOptions = new GraphQLHttpClientOptions
    {
        EndPoint = new Uri($"{Url}/graphql", UriKind.Absolute)
    };

    var graphQLHttpClient = new GraphQLHttpClient(graphQLOptions, new NewtonsoftJsonSerializer());

    var documentRequest = new GraphQLRequest { Query = iQuery };

    try
    {
        var graphQLResponse = await graphQLHttpClient.SendQueryAsync<RootObject>(documentRequest);
        Debug.Print("");
        return null;
    }
    catch (Exception ex)
    {
        Debug.Print(ex.Message);
        return null;
    }

}
C# GraphQL Maui

评论


答: 暂无答案