Web服务调用循环/未完成

时间:2013-07-16 14:48:15

标签: c# visual-studio-2012 windows-store-apps

对于我的应用程序,我调用Web服务来获取客户数据。我遇到的问题是,当我进行此特定调用时,它将获得异步await调用并在不完成调用的情况下循环返回,然后存储结果。

private void DatabaseTest()
{
    cNum = Convert.ToString(db.selectCustomerNumber());
    callC = "SELECT * FROM dashboardCustomer WHERE period = 'C' AND customerNumber = " + cNum;
    callB = "SELECT * FROM dashboardCustomer WHERE period = 'B' AND customerNumber = " + cNum;
    callF = "SELECT * FROM dashboardCustomer WHERE period = 'F' AND customerNumber = " + cNum;
    if (db.selectDashboard(callC).Count == 0)
    {
        GetDataSummary(passC);
    }
    if (db.selectDashboard(callB).Count == 0)
    {
        GetDataSummary(passB);
    }
    if (db.selectDashboard(callF).Count == 0)
    {
        GetDataSummary(passF);
    }
}

    private async void GetDataSummary(string r)
    {

        long customerNum = db.selectCustomerNumber();
        pin = db.selectPinByCustomerNumber(customerNum);

        string cType = r;
        try
        {
            Windows.Security.Credentials.PasswordVault vault = new Windows.Security.Credentials.PasswordVault();
            IReadOnlyList<PasswordCredential> userCredential = vault.FindAllByResource(pin);

            userCredential[0].RetrievePassword();

            try
            {
                getCustomerBillSummaryResponse billSum = await
                    UBPclient.getCustomerBillSummaryAsync(userCredential[0].UserName, userCredential[0].Password, customerNum, cType);

                invoiceSummaryBean[] summaryList = billSum.@return;

                rh.DashboardHandler(summaryList, customerNum);


            }
            catch
            {

            }
        }
        catch
        {

        }

    }

它将运行到以下部分

getCustomerBillSummaryResponse billSum = await
                    UBPclient.getCustomerBillSummaryAsync(userCredential[0].UserName, userCredential[0].Password, customerNum, cType);

然后循环回到try并再次运行,直到它运行了三次。

如何让它为每次调用返回它所假设的数据并将其存储在我的数据库中?

此外,我在SoapUI中测试了Web服务,并且调用返回结果,因此问题不在于Web服务。

2 个答案:

答案 0 :(得分:2)

你需要这样做:

private async Task GetDataSummary(string r)

您需要返回Task而不是void,因为您的来电者需要等待一些事情。当您返回void时,调用者必须将方法视为“即发即忘”。当您返回Task时,调用者可以为await创建必要的代码,以便完成异步方法。

请不要忘记在调用时添加await关键字:await GetDataSummaryAsync(...);

答案 1 :(得分:1)

你应该避免async void。将GetDataSummary更改为Task,然后await更改DatabaseTest

private async Task DatabaseTestAsync()
{
    ...
    if (db.selectDashboard(callC).Count == 0)
    {
        await GetDataSummaryAsync(passC);
    }
    if (db.selectDashboard(callB).Count == 0)
    {
        await GetDataSummaryAsync(passB);
    }
    if (db.selectDashboard(callF).Count == 0)
    {
        await GetDataSummaryAsync(passF);
    }
}

private async Task GetDataSummaryAsync(string r)