eridanix
eridanix

Reputation: 828

How to call web service programmatically from Silverlight?

I'am trying to access WebService from Silverlight and call some its method.

I tried to use example from: http://www.codeproject.com/Tips/394436/Calling-a-webservice-programmatically

// In Silverlight it is not posible to use this 
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

But it is not applicable for Silverlight application. Do you have any example how to connect to WebService from Silverlight and then call its method?

Upvotes: 2

Views: 942

Answers (1)

Gene
Gene

Reputation: 4232

Silverlight doesn't allow you to perform IO task synchronously to keep your application responsive. That's why GetResponse() ist not available. You need to get the response asynchronously:

WebRequest request = WebRequest.Create(/* URI */);
request.BeginGetResponse(HandleResponse, request);

The delegate you pass to the method will be invoked when the repsonse is available. Example implementation of this method:

private void HandleResponse(IAsyncResult result)
{
  WebRequest request = (WebRequest)(result.AsyncState);

  using (var response = request.EndGetResponse(result))
  {
    // do something with the response
  }
}

Upvotes: 2

Related Questions