如果调用两次,UWP Wcf服务调用异步会失败

时间:2016-08-04 03:30:16

标签: c# wcf uwp

我不理解的东西正在我的逻辑中发生,我没有得到任何期望,但我的申请被锁定了。

代码:

ChatClient Service = new ChatClient();
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        var temp = Service.AuthenicateAsync(T1.Text, T2.Text).Result;
    }

ChatClient代码:

public User Authenicate(string username, string password)
    {
        Clock.Restart();

        var User = new User();
        var Valid = false;

        try
        {

            var VerificationQuery = Provider.CreateAdoCommand("select count(AccessToken) as 'Verified' from Users where Email=@user and Password=@pass;", Provider.CreateParameter("@user", DbType.String), Provider.CreateParameter("@pass", DbType.String));
            var reader1 = VerificationQuery.ExecuteReader(username, password);
            using (reader1.Open())
            {//Check to see if the username and password is valid
                while (reader1.MoveNext())
                {
                    if (reader1.GetInt32("Verified").Value == 1)
                    {
                        Valid = true;
                    }
                }
            }

            if (Valid)
            {
                var TokenQuery = Provider.CreateAdoCommand("select AccessToken, Timeout from Users where Email=@user and Password=@pass", Provider.CreateParameter("@user", DbType.String), Provider.CreateParameter("@pass", DbType.String));
                var reader2 = TokenQuery.ExecuteReader(username, password);
                using (reader2.Open())
                {//If the username and password is valid, retrieve the token and timeout period
                    while (reader2.MoveNext())
                    {
                        User.AccessToken = reader2.GetString("AccessToken");
                        User.TimeoutPeriod = reader2.GetDateTime("Timeout").Value;
                    }
                }

                //Check to see how old the timeout period is
                var timeSub = User.TimeoutPeriod.Subtract(DateTime.Now);
                if (timeSub.Days < 2)
                {
                    //The token is too old, update the token, old token and timeout period
                    var TokenUpdateQuery = Provider.CreateAdoCommand("update Users set AccessToken=(NEWID()), OldAccessToken=@oldToken, Timeout=@newTime", Provider.CreateParameter("@oldToken", DbType.String), Provider.CreateParameter("@newTime", DbType.Date));
                    using (TokenUpdateQuery.Open())
                    {
                        TokenUpdateQuery.ExecuteNonQuery(User.AccessToken, DateTime.Now.AddDays(30));
                    }

                    var FinalQuery = Provider.CreateAdoCommand("select AccessToken, Timeout from Users where Email=@user and Password=@pass", Provider.CreateParameter("@user", DbType.String), Provider.CreateParameter("@pass", DbType.String));
                    var reader3 = FinalQuery.ExecuteReader(username, password);
                    using (reader3.Open())
                    {
                        while (reader3.MoveNext())
                        {
                            User.AccessToken = reader3.GetString("AccessToken");
                            User.TimeoutPeriod = reader3.GetDateTime("Timeout").Value;
                        }
                    }
                }

                //Database Query for user information
                var UserQuery = Provider.CreateAdoCommand("select FirstName, LastName, Email, DateCreated, AccessToken, OldAccessToken, Timeout, ConnectionId from [Users] where AccessToken=@token;", Provider.CreateParameter("@token", DbType.String));
                var reader4 = UserQuery.ExecuteReader(User.AccessToken);

                using (reader4.Open())
                {
                    while (reader4.MoveNext())
                    {
                        if (reader4.GetString("AccessToken").Equals(User.AccessToken))
                        {
                            User.FirstName = reader4.GetString("FirstName");
                            User.LastName = reader4.GetString("LastName");
                            User.Email = reader4.GetString("Email");
                            User.AccessToken = reader4.GetString("AccessToken");
                            User.OldAccessToken = reader4.GetString("OldAccessToken");
                            User.TimeoutPeriod = reader4.GetDateTime("Timeout").Value;
                            User.DateCreated = reader4.GetDateTime("DateCreated").Value;
                            User.ConnectionId = reader4.GetString("ConnectionId");
                        }
                    }
                }
            }
        }
        catch (Exception)
        {

        }

        Clock.Stop();
        User.SecondsProcessed = Clock.Elapsed.TotalSeconds;
        return User;

    }

服务是托管在我的Web服务器上的WCF服务,该服务是使用Visual Studio添加的。在我的应用程序中,我有两个文本框和一个按钮。单击该按钮将触发此方法。它第一次运作。再次单击它并在断点后点击var temp ...但是然后只是关闭并且我的应用程序锁定。

1 个答案:

答案 0 :(得分:0)

我实在无法解释。但是改变方法看起来像这样。我的意思是我可以,因为正在调用Async方法,你需要等待它来获取数据。因此,需要将async关键字添加到方法中。

html