Xamarin Forms PCL 使用 WCF 并不总是 return 一个值
Xamarin Forms PCL Consuming WCF does not always return a value
我有一个简单的 WCF 服务,用于验证登录和 return 代表用户的对象。
它正在 运行 Xamarin Forms PCL 项目中。
当我单击登录按钮时,第一次 returned 对象 return 为 null,但第二次按下时 return 为用户对象。
正在完成方法的异步函数中创建对象。
我不是 qui确定我错过了什么。
这里有一些代码可以帮助澄清。
感谢所有帮助!
点击按钮调用的函数...
private void buildLoginUser(string strUserName, string strPassword)
{
if (strUserName == null || strPassword == null) {
strResult = "Please Enter User Name and Password.";
DisplayAlert("Alert",strResult,"OK");
} else {
//IF USERNAME AND 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");
}
}
build用户对象的代码...
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;
}
}
谢谢!我更改了代码,现在出现 UI 线程错误...
ui套件一致性错误您正在调用一个只能从 ui 线程调用的 ui套件方法。
private async void buildLoginUser(string strUserName, string strPassword)
{
if (strUserName == null || strPassword == null) {
strResult = "Please Enter User Name and Password.";
DisplayAlert("Alert",strResult,"OK");
} else {
//IF USERNAME AND 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");
}
});
}
}
您需要将您的方法变成 Async
方法。
目前您的 ValidateUserAsync
方法 returns 直接表示 lu.FullName
为 null 或空。
要将您的方法变成异步方法,请将单词 aync
添加到方法中,将 await
添加到您想要等待的异步行中。
private async void buildLoginUser(string strUserName, string strPassword)
{
if (strUserName == null || strPassword == null) {
strResult = "Please Enter User Name and Password.";
DisplayAlert("Alert",strResult,"OK");
} else {
//IF USERNAME AND 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");
}
}
}
我有一个简单的 WCF 服务,用于验证登录和 return 代表用户的对象。
它正在 运行 Xamarin Forms PCL 项目中。 当我单击登录按钮时,第一次 returned 对象 return 为 null,但第二次按下时 return 为用户对象。
正在完成方法的异步函数中创建对象。
我不是 qui确定我错过了什么。
这里有一些代码可以帮助澄清。
感谢所有帮助!
点击按钮调用的函数...
private void buildLoginUser(string strUserName, string strPassword)
{
if (strUserName == null || strPassword == null) {
strResult = "Please Enter User Name and Password.";
DisplayAlert("Alert",strResult,"OK");
} else {
//IF USERNAME AND 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");
}
}
build用户对象的代码...
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;
}
}
谢谢!我更改了代码,现在出现 UI 线程错误... ui套件一致性错误您正在调用一个只能从 ui 线程调用的 ui套件方法。
private async void buildLoginUser(string strUserName, string strPassword)
{
if (strUserName == null || strPassword == null) {
strResult = "Please Enter User Name and Password.";
DisplayAlert("Alert",strResult,"OK");
} else {
//IF USERNAME AND 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");
}
});
}
}
您需要将您的方法变成 Async
方法。
目前您的 ValidateUserAsync
方法 returns 直接表示 lu.FullName
为 null 或空。
要将您的方法变成异步方法,请将单词 aync
添加到方法中,将 await
添加到您想要等待的异步行中。
private async void buildLoginUser(string strUserName, string strPassword)
{
if (strUserName == null || strPassword == null) {
strResult = "Please Enter User Name and Password.";
DisplayAlert("Alert",strResult,"OK");
} else {
//IF USERNAME AND 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");
}
}
}