Friday, September 7, 2012

Consuming WCF web service in Silverlight

Consuming WCF web service in Silverlight

I am not able to retrieve the result from the WCF web service. The result is always null.

Here is my Silverlight client code:

TTServiceClient client1 = new TTServiceClient();             client1.GetUserNameCompleted += new EventHandler<GetUserNameCompletedEventArgs>(client1_GetUserNameCompleted);             client1.GetUserNameAsync();  void client1_GetUserNameCompleted(object sender, GetUserNameCompletedEventArgs e)         {             txtUserName.Text = e.Result;         } 

My web service implementation looks like:

public string GetUserName() {      return System.Web.HttpContext.Current.User.Identity.Name.ToString();  }  

But I am able to get the result in wcf service.

Answers & Comments...

Answer: 1

The Silverlight webservice will not throw any exceptions regardless if the request was successful or not. In your callback you should, however, always check for errors the eventargs e.Errors, in your case something like:

void client1_GetUserNameCompleted(object sender, GetUserNameCompletedEventArgs e) {    if(e.Error != null)    {        throw new Exception("Web service error: " + e.Error.Message);    }    else    {        txtUserName.Text = e.Result;    } } 

Supposedly e.Error will tell you more about the problem.

by : Avada Kedavrahttp://stackoverflow.com/users/280104




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog