SpriteHand
Module Border
  Calling an .asmx Web Service in Silverlight 2
Module Border
Location: BlogsAndy's Blog    
Posted by: host 3/17/2008 6:14 PM

I wanted to share a couple of tips for calling web services from a Silverlight client.

If you are just beginning to useWeb Services from a Silverlight 2 Client, Tim Heuer has a good introductory post here.

Getting the Binding Url
A lot of the web service samples will show the binding created with a hardcoded Uri such as this:

WebServiceSoapClient webSvc = new WebServiceSoapClient(binding, new System.ServiceModel.EndpointAddress("http://localhost/MyWebService.asmx"));

The problem being of course that the EndpointAddress is hardcoded to localhost, and you may not know what server you will ultimately deploy to. You could add the Uri as a configuration setting, but there is a more flexible way.

Chances are, your web service is hosted on the same web server that your Silverlight application downloads from. So when you create your Web Service binding, you can determine the Url for you service to bind to based on the current browser Uri:

public static string GetUrlForResource(string resourcePage)
{
    string webUrl = System.Windows.Browser.HtmlPage.Document.DocumentUri.ToString();

    string containerPage = webUrl.Substring(webUrl.LastIndexOf("/") + 1);

    webUrl = webUrl.Replace(containerPage, resourcePage);

 

    return webUrl;

}

... So the method above, when passed a resource string such as "MyWebService.asmx" will return a full Url for a page that is in the same virtual directory as the silverlight app.

When binding, you can then use this utility method like so:

// get the full url of the Web Service
string
webServiceUrl = GetUrlForResource("WebService.asmx");

System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding();

WebServiceSoapClient webSvc = new WebServiceSoapClient(binding, new System.ServiceModel.EndpointAddress(webServiceUrl));

webSvc.HelloWorldCompleted += new EventHandler<HelloWorldCompletedEventArgs>(webSvc_HelloWorldCompleted);
webSvc.HelloWorldAsync();

Keep an Eye on MaxReceivedMessageSize
If your web service is returning a lot of data, you may blow out the default max message size for a return. If this happens you will get an exception like so:

An exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll but was not handled in user code

Additional information: [MaxReceivedMessageSizeExceeded]

To remedy this, you can up the MaxReceivedMessageSize attribute of the binding like so:

System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding();

// if you are getting a LOT of data back, you will need to up Message Size
binding.MaxReceivedMessageSize = int.MaxValue;

Permalink |  Trackback

Comments (5)   Add Comment
Re: Calling an .asmx Web Service in Silverlight 2    By Anonymous on 3/19/2008 2:37 AM
Why do we need to use BasicHttpBinding or etc while are are able to add as service reference simplye?

I wrote one sample in this post below and I have no problem in using it.
http://michaelsync.net/2008/03/10/silverlight-2-beta1-database-operations-with-aspnet-web-service-in-silverlight-2


Re: Calling an .asmx Web Service in Silverlight 2    By admin on 3/19/2008 10:44 AM
The reason you need to add a binding with an EndPoint is for deployment to another server besides your test project. Because if you look at the generated Proxy code (by expanding the Service reference in solution explorer until you find Reference.cs), you will see that the Uri is hardcoded for your test web service. It will look something like this:

private static System.ServiceModel.EndpointAddress defaultAddress = new System.ServiceModel.EndpointAddress("http://localhost:58920/MySLApp_Web/MyService.svc");


... so when you go to actually deploy this SL app and web service, this default binding will no longer work.

Re: Calling an .asmx Web Service in Silverlight 2    By Anonymous on 4/25/2008 2:10 AM
not with the portnumber how can we call webservice using localhost
(iis)

Re: Calling an .asmx Web Service in Silverlight 2    By Anonymous on 6/2/2008 2:35 AM
Hi,

Nice article!

I tried this one out but my web service is returning more than 65000 rows. I already increased the MaxReceivedMessageSize to int's maxvalue. But I'm getting this error:

An exception of type 'System.ServiceModel.ProtocolException' occurred in System.ServiceModel.dll but was not handled in user code

Additional information: [UnexpectedHttpResponseCode]
Arguments:Not Found
Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=2.0.30226.2&File=System.ServiceModel.dll&Key=UnexpectedHttpResponseCode

But if I would filter the data being returned from the web service, it would not throw an exception.

Do you have any idea on this?

Thanks.

Re: Calling an .asmx Web Service in Silverlight 2    By Anonymous on 6/2/2008 8:04 AM
You could try using an HTTP tracing tool such as Fiddler or Nikhil's Web Development Helper to get a capture of the response coming back. There is a good chance that will contain better exception information than what you are seeing.

-Andy


Title:
Comment:
Add Comment   Cancel 
Module Border Module Border
Module Border
  Blog_List
Module Border
Module Border Module Border
Module Border
  Subscribe
Module Border

RSS

Module Border Module Border
Module Border
  Diversions
Module Border

DESTROY ALL INVADERS
A scrolling shooter game where the objective is to destroy the invading UFO's flying over a neighborhood of your choosing. Imagery provided by Microsoft Virtual Earth. Created using Silverlight 2.
PLAY IT

INFO AND CODE



SORT THE FOOBARS
A game where you need to sort the good foobars from the bad ones. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



POLYGON PHYSICS DEMO
A demo showing polygon physics where the user draws physics objects with the mouse. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SILVERLIGHT ROCKS!
Destroy the asteroids before they destroy your ship! Created using Silverlight 2.
PLAY IT

INFO AND CODE



FISH GAME
A simple game of harpoon-the-fish. Written using the AJAX Sprite Toolkit.
PLAY IT

INFO AND CODE

Module Border Module Border
Module Border
  Search_Blog
Module Border
Module Border Module Border
Module Border
  Blog_Archive
Module Border
Module Border Module Border
Copyright (c) 2008 andy.beaulieu.com - Login