c# - Xamarin Forms PCL Consuming WCF does not always return a value -
i have simple wcf service validating login , return object representing user.
it being run xamarin forms pcl project. when click login button, returned object returns null first time, when pressing second time returns user object.
the object being created in async functions on completed method.
i not quite sure missing.
here pieces of code clarify.
thanks , assistance!
the function called clicking button...
private void buildloginuser(string strusername, string strpassword) { if (strusername == null || strpassword == null) { strresult = "please enter user name , password."; displayalert("alert",strresult,"ok"); } else { //if username , password received validate against db //cl.validateuserasync (strusername, strpassword); //strresult = struserrole; buildclientservice bcs = new buildclientservice (); //authenticateuser au = new authenticateuser (); tdmsclient cl = new tdmsclient (); cl = bcs.initializeserviceclient(); cl.validateuserasync(strusername, strpassword); //au.authenticateuserlogin (strusername, strpassword, cl); //displayalert("new user", lu.fullname, "ok"); if (lu.fullname == null || lu.fullname == "") { displayalert ("alert", "null", "ok"); } else { displayalert ("alert", lu.fullname, "ok"); } }
the code builds user object...
public class buildclientservice { public static readonly endpointaddress endpoint = new endpointaddress("http://wcf.thompsoncs.net/tdms.svc"); public string struserrole = ""; basichttpbinding binding = createbasichttp(); private static basichttpbinding createbasichttp() { basichttpbinding binding = new basichttpbinding { name = "basichttpbinding", maxbuffersize = 2147483647, maxreceivedmessagesize = 2147483647 }; timespan timeout = new timespan(0, 0, 30); binding.sendtimeout = timeout; binding.opentimeout = timeout; binding.receivetimeout = timeout; return binding; } public tdmsclient initializeserviceclient(){ tdmsclient cl = new tdmsclient(); cl = new tdmsclient (binding, endpoint); cl.validateusercompleted += clonvalidateusercompleted; return cl; } private void clonvalidateusercompleted(object sender, validateusercompletedeventargs validateusercompletedeventargs) { tdms.login.lu = validateusercompletedeventargs.result; } }
thanks! changed code , getting ui thread error... uikit consistency error calling uikit method can invoked ui thread.
private async void buildloginuser(string strusername, string strpassword) { if (strusername == null || strpassword == null) { strresult = "please enter user name , password."; displayalert("alert",strresult,"ok"); } else { //if username , password received validate against db buildclientservice bcs = new buildclientservice (); tdmsclient cl = new tdmsclient (); cl = bcs.initializeserviceclient(); var task = task.factory.startnew(() => cl.validateuserasync(strusername, strpassword)); await task.continuewith (e => { if (lu.fullname == null || lu.fullname == "") { displayalert ("alert", "null", "ok"); } else { displayalert ("alert", lu.fullname, "ok"); } }); } }
you need make method async
method.
at moment validateuserasync
method returns straight away meaning lu.fullname
null or empty.
to turn method async method add word aync
method , await
async line await.
private async void buildloginuser(string strusername, string strpassword) { if (strusername == null || strpassword == null) { strresult = "please enter user name , password."; displayalert("alert",strresult,"ok"); } else { //if username , password received validate against db //cl.validateuserasync (strusername, strpassword); //strresult = struserrole; buildclientservice bcs = new buildclientservice (); //authenticateuser au = new authenticateuser (); tdmsclient cl = new tdmsclient (); cl = bcs.initializeserviceclient(); await cl.validateuserasync(strusername, strpassword); //au.authenticateuserlogin (strusername, strpassword, cl); //displayalert("new user", lu.fullname, "ok"); if (string.isnullorempty(lu.fullname)) { displayalert ("alert", "null", "ok"); } else { displayalert ("alert", lu.fullname, "ok"); } } }
Comments
Post a Comment