[英]Web Service Call Loop/Not Finishing
For my app I make calls to a web service to get customer data. 对于我的应用程序,我调用了Web服务以获取客户数据。 The problem I am encountering is when I make this particular call it gets the the asynchronous await call and loops back without finishing the call and then storing the results.
我遇到的问题是,当我进行此特定调用时,它获取了异步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
{
}
}
it runs to the following part 它运行到以下部分
getCustomerBillSummaryResponse billSum = await
UBPclient.getCustomerBillSummaryAsync(userCredential[0].UserName, userCredential[0].Password, customerNum, cType);
and then loops back to the try and runs again until it has ran three times. 然后循环回到尝试并再次运行,直到运行了三次。
How do I make it return the data it is suppose to for each call and store it in my database? 如何使它返回每个调用的假定数据并将其存储在数据库中?
Also I have tested the web service in SoapUI and the call is returning results, so the problem is not with the web service. 另外,我已经在SoapUI中测试了Web服务,并且该调用返回了结果,所以问题不在于Web服务。
You need to do this: 您需要这样做:
private async Task GetDataSummary(string r)
You need to return Task
instead of void
because your caller needs to have something to wait for. 您需要返回
Task
而不是void
因为调用者需要等待一些东西。 When you return void
, the caller must treat method as "fire-and-forget". 当您返回
void
,调用者必须将方法视为“即发即弃”。 When you return Task
, the caller can create the necessary code to await
for async method to finish. 当您返回
Task
,调用者可以创建必要的代码,以await
异步方法完成。
And don't forget to add the await keyword when you call it: await GetDataSummaryAsync(...);
并且不要忘记在调用它时添加await关键字:
await GetDataSummaryAsync(...);
You should avoid async void
. 您应该避免
async void
。 Change GetDataSummary
to return Task
and then await
it from DatabaseTest
: 更改
GetDataSummary
以返回Task
,然后从DatabaseTest
await
它:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.