【问题标题】:Xamarin Forms PCL Consuming WCF does not always return a valueXamarin Forms PCL Consuming WCF 并不总是返回值
【发布时间】:2015-04-30 03:30:52
【问题描述】:

我有一个简单的 WCF 服务,它正在验证登录并返回一个代表用户的对象。

它正在与 Xamarin Forms PCL 项目一起运行。 当我点击登录按钮时,第一次返回的对象返回null,但第二次按下时返回用户对象。

对象正在完成方法的异步函数中创建。

我不太确定我错过了什么。

这里有一些代码可以帮助澄清。

感谢您的所有帮助!

点击按钮调用的函数...

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");
            }

        }

构建用户对象的代码...

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 线程错误... uikit 一致性错误 你正在调用一个只能从 ui 线程调用的 uikit 方法。

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");
                }
            });

        }

    }

【问题讨论】:

    标签: c# wcf xamarin xamarin.ios


    【解决方案1】:

    你需要把你的方法变成Async方法。

    此时您的ValidateUserAsync 方法会立即返回,这意味着lu.FullName 为空或为空。

    要将您的方法转换为异步方法,请将单词 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");
            }
    
        }
    }
    

    Here's some more information and examples on Async/Await

    【讨论】:

    • 谢谢,我试过了,但我收到一条错误消息,指出“无法等待 'void' 表达式。” WCF 的代理为此函数创建了一个 void 表达式。
    猜你喜欢
    • 2021-12-26
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多