Mike Taulty's Blog
Bits and Bytes from Microsoft UK
Silverlight V1.1 & Web Services in VS2008

Blogs

Mike Taulty's Blog

Elsewhere

Archives

I've seen a few questions about how to do this so I thought I'd note how I've been calling my web service from my Silverlight 1.1 app during development.

I'm not sure if this is the sanctioned or right way to do it but it's been working for me.

Steps as below;

  1. File->New->Web Site. I've been working with Cassini so I just put the website into the file system.
  2. Delete default.aspx as we're building a service here.
  3. Add a new web service (WebService.asmx).
  4. Implement your service (e.g.);
[WebService(Namespace = "http://tempuri.org/")]
[ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () 
    {
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string HelloWorld() {
        return "Hello World";
    }
    
}

Now we have a service, let's build something from Silverlight to call it;

  1. File->Add->New Project->Silverlight Project
  2. Now, add a web reference to the web service from the Silverlight project. Note: If you're using Cassini then I'd suggest setting it to run up on a fixed port at this point by changing your web site properties otherwise your web reference will break from time to time. Property sheet below show dynamic ports is set to false and port number is set to 65534.

image

  1. Add a web reference from the Silverlight Project to the web service (for me this is to http://localhost:65534/WebSite2/WebService.asmx?wsdl) - you'll perhaps have to spin up your web service project first in order to get Cassini sitting and listening for traffic (i.e. setting your .asmx file as the start page and pressing CTRL F5 should do that).
  2. Write some code into your Silverlight app (e.g.);

 

<Canvas x:Name="parentCanvas"
        xmlns="http://schemas.microsoft.com/client/2007" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Loaded="Page_Loaded" 
        x:Class="SilverlightProject1.Page;assembly=ClientBin/SilverlightProject1.dll"
        Width="640"
        Height="480"
        Background="White"
        >
  <TextBlock x:Name="myText" Width="192" Height="48" FontSize="24"/>
</Canvas>
 
  public partial class Page : Canvas
  {
    public void Page_Loaded(object o, EventArgs e)
    {
      // Required to initialize variables
      InitializeComponent();

      WebService service = new WebService();
      myText.Text = service.HelloWorld();
    }
  }

 

Now, when it comes to debugging it seems that running a Silverlight project just loads the page from the file system and I'm not sure whether that's going to work when your page is calling a service that comes from elsewhere so what I've been doing is;

  1. Add a link from the Web Site project to the Silverlight Project using the "Add Silverlight Link" menu option off the right mouse menu when you right mouse on your web project. This will get your assembly, PDB and .XAML files copied to the web site and kept in sync (careful not to edit the XAML file in the wrong place after doing this - it's bitten me more than once and if you edit the one in the web folder then it'll get overwritten on each build).

image

With that in place;

  1. Copy TestPage.html, TestPage.html.js, Silverlight.js from your Silverlight project to your web site project.
  2. Set TestPage.html (in the web site project) as your start page.
  3. Hit F5 (take care to note that your Web Site project is running up and the file is coming from http://localhost and not the file system which it will do if you run the Silverlight project).

You should be in business. If you want to make your call to the web service survive a redeployment then do something like;

    public void Page_Loaded(object o, EventArgs e)
    {
      // Required to initialize variables
      InitializeComponent();

      WebService service = new WebService();

      service.Url = (new Uri(HtmlPage.DocumentUri, "WebService.asmx")).ToString();

      myText.Text = service.HelloWorld();
    }

or something more elaborate :-)


Posted Wed, Aug 8 2007 4:31 PM by mtaulty

Comments

Debugging Silverlight via IIS « The Pursuit of a Life wrote Debugging Silverlight via IIS &laquo; The Pursuit of a Life
on Mon, Dec 17 2007 8:00 PM