Irakli Lekishvili
Irakli Lekishvili

Reputation: 34158

silverlight enabled wcf service retuning value

This is my service which checks username and password

   [OperationContract]

    public bool LoginCheck(string username, string password) 
    {
        RoadTransDataContext db = new RoadTransDataContext();

        var _Pass = (from d in db.users where d.username == username select d.password).SingleOrDefault();

        if (_Pass == password)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

And this is child window

 private void LoginCheckCompleted(object sender, ServiceReference.LoginCheckCompletedEventArgs e)
    {
        _Log = e.Result;
    }

    private void OKButton_Click(object sender, RoutedEventArgs e)
    {
        ServiceReference.ServiceClient webservice = new ServiceReference.ServiceClient();

        webservice.LoginCheckCompleted += new EventHandler<ServiceReference.LoginCheckCompletedEventArgs>(LoginCheckCompleted);
        webservice.LoginCheckAsync(txtUserName.Text, txtPassword.Password);

        if (_Log == true)
        {
            this.DialogResult = true;
            this.Close();
        }
    }

problem is that LoginCheckCompleted method is calling when OKButton_Click method finished. so if it input correct username, pass and press button it doing nothing if i click onece again window closing

Upvotes: 0

Views: 91

Answers (1)

vortexwolf
vortexwolf

Reputation: 14037

Silverlight uses the async model of invoking web services and it takes some time to wait until the response is returned. In your example the assigment _Log = e.Result; will be called, let's assume, after 1-2 seconds, whereas the check if (_Log == true) will be called immideately and of course before the assignment.

That's why you should put all the necessary code in the callback and remove all the code after the async call. I've fixed it for you:

private void LoginCheckCompleted(object sender, ServiceReference.LoginCheckCompletedEventArgs e)
{
    _Log = e.Result;

    if (_Log == true)
    {
        this.DialogResult = true;
        this.Close();
    }
}

private void OKButton_Click(object sender, RoutedEventArgs e)
{
    ServiceReference.ServiceClient webservice = new ServiceReference.ServiceClient();

    webservice.LoginCheckCompleted += new EventHandler<ServiceReference.LoginCheckCompletedEventArgs>(LoginCheckCompleted);
    webservice.LoginCheckAsync(txtUserName.Text, txtPassword.Password);
}

Upvotes: 2

Related Questions