I 'm developing a program using webservice. Data is given asynchronously. (refer TodoASMX )
Program sources are shown below.
how to use task and await?
Webservice
[WebMethod]
public userInfo WS_PDA010E01_R1(string strID, string strPWD)
{
DataTable dt = new DataTable();
userInfo uInfo = new userInfo();
try
{
uInfo = todoService.CMWS_WS_PDA010E01_R1(strID, strPWD);
return uInfo;
}
catch (Exception ex )
{
throw new SoapException("Error", SoapException.ServerFaultCode, ex);
}
}
**Forms **
1. public interface ISoapService
- Task WS_PDA010E01_R1(string strID, string strPWD);
2. public class DataManager
- public Task WS_PDA010E01_R1(string strID, string strPWD)
{
return soapService.WS_PDA010E01_R1(strID, strPWD);
}
3. XAML
async void OnLoginButtonClicked(object sender, EventArgs e)
{
try
{
var items = await App.dtManager.WS_PDA010E01_R1(usernameEntry.Text, passwordEntry.Text);
if (string.IsNullOrEmpty(items.MEMBERNAME) == false)
{
App.IsUserLoggedIn = true;
await Navigation.PopModalAsync(true);
}
else
{
App.IsUserLoggedIn = false;
messageLabel.Text = "Login failed";
passwordEntry.Text = string.Empty;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Andoid
public async Task WS_PDA010E01_R1(string strID, string strPWD)
{
var varUser = new userInfo();
try
{
//Error-generation part
var items = await Task.Factory.FromAsync(begin., commonWS.WS_PDA010E01_R1, null, TaskCreationOptions.None);
varUser.MEMBERID = items.MEMBERID;
varUser.MEMBERNAME = items.MEMBERNAME;
return varUser;
}
catch (Exception ex)
{
throw new Exception(ex);
}
}