mihai
mihai

Reputation: 2804

Is there any way to simplify WebService's Async call

I'm new to Silverlight development. I write a mini test application which authentificates on a server. The communication i made through WCF WebServices.

To use the WebService's Login(string, int) method i must everytime use this sequence

1) initialize the a data member with a EventHandler

_ltsCl.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(_ltsCl_LoginCompleted);

where var _ltsCl = new LoginToServerServiceClient();

and LoginToServerService is the WebService class

2) invoke the Async method

_ltsCl.LoginAsync(txtUsername.Text, int.Parse(pbxPassword.Password));

3) get results with

_ltsCl_LoginCompleted(object sender, LoginCompletedEventArgs e){}

Is it possible to simplify this sequence in a way something like this

bool result = _ltsCl.Login(txtUsername.Text, int.Parse(pbxPassword.Password));

Upvotes: 0

Views: 126

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

You could define the complete callback as an anonymous method:

_ltsCl.LoginCompleted += (sender, e) => 
{
    var result = e.Result;
};
_ltsCl.LoginAsync(txtUsername.Text, int.Parse(pbxPassword.Password));

The syntax you have shown:

bool result = _ltsCl.Login(txtUsername.Text, int.Parse(pbxPassword.Password));

is possible only with the synchronous call which blocks the calling thread during the processing and returns the result once this processing has completed. It doesn't make any sense for asynchronous methods which return the control to the calling thread immediately.

But since synchronous blocking calls are forbidden in Silverlight you don't even have this possibility.

Upvotes: 2

Related Questions