Calling web services from your Silverlight alpha app

Currently, you”ll only get tool support for ASMX web services that render JSON. This doesn”t mean that you can”t call a traditional ASMX web service (or even a WCF service). It only means that you”ll need to handle the XML yourself. Today, I”m only going to talk about my ASMX web service calls tests. So, if you have a ASMX web service, you need to:

1. remove the traditional asmx handle and add the new ScriptHandlerfactory that was introduced by the ASP.NET AJAX extensions. If you”re using IIS 6, this means that you have to add something like this to the handlers section of your web.config file:

<httpHandlers>
        <remove verb=”*” path=”*.asmx”/>
        <add verb=”*” path=”*.asmx” validate=”false”
            type=”System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″/>

</httpHandlers>

2. You need to decorate your web service class with the ScriptServiceAttribute and your public callable methods with the ScriptMethodAttribute attribute.

3. You should create a proxy to call the web service by using the slwsdl.exe tool. If you have VS orcas, then just add a reference to your web service (it”ll do the right thing)

4. Don”t forget to update the Url property of the proxy so that you don”t get a cross-domain exception. Don”t forget that in the current release you can only call “local” web services (and yes, different ports means a cross-domain call). Getting a cross-domain exception might happen more easily than you expect. For instance,this might happen if you”re using the internal server to build the site that hosts the silverlight control and you”ve added a reference to the web service through VS orcas (in my test,it just hard-coded the port that was being used by the internal server and then i got into trouble when i accessed it through a localhost call – which went through the IIS server). Since you must put everything on the same web site, why not play safe and just add something like this in your C# code before calling the method:

webServiceReference.Url = “relative_path_to_your_web_service.asmx”;

4. If you can, use async services. Even though the current release”s support for multithreading sucks, we can call a web service asynchronously and then update the user interface without getting an exception (which means that the callback is being called on the correct thread). 

And you should be ready to go.

Advertisements

~ by Luis Abreu on June 11, 2007.

4 Responses to “Calling web services from your Silverlight alpha app”

  1. Hi,

    I don”t quite get everything that you say. Can I get a full sample?

    My email address is CheeSiang.Sim@acs-inc.com

  2. Hello there, I am currently writing my thesis on rich web apps with silverlight… the 4th point( calling cross domain..) is actually burning me up… i tried to test to download an image from another server, not hosted on my machine and silverlight complained about runtime error…How can i pass through this..?
    samuelbalcha@hotmail.com

  3. im getting cross domain calls are not supported byHttpWebrequest error.how to resolve this error

  4. add a crossdomain.xml file to the target server

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: