Silverlight Tip of the Day #42: How to Create a Web Service for your Silverlight App
Web services are essentially API’s that can be accessed over a network such as the Internet. Due to security Silverlight restricts a lot of important features such as saving to the disk. However, you can get around these restrictions by making calls to web services instead to accomplish these features.
This Tip of the Day will step you through how to create a web service in your web site and I will show you how to reference and make calls to API’s in that web service.
Step 1. Create a Silverlight Application Project using Visual Studio 2008. (See Tip of the Day #2 for details).
Step 2. Add a Web Service to your Web Site.
- In your Solution Explorer, right click on your Web Site node, Choose Add-> New Item…
- There are 3 templates that offer Web Service functionality:
- Silverlight-enabled WCF Service.
- WCF Service
- Web Service.
Choose “Silverlight-enabled WCF Service” as it has specific support for Silverlight which we will explore in our next Tip of the Day.
- Change the name to something like “MyService.asmx” and click the “Add” button.
Step 3. Add a Service Reference
- Rebuild your Solution (Ctrl+Shift+B). If you proceed to the next bullet point without rebuilding you will get the following dialog:
- In your solution explorer, right click on your Silverlight Application node and choose “Add Service Reference…”.
- This will bring up the “Add Service Reference” dialog. Click the “Discover” button to find your web service.
- Change the Namespace to be whatever you want for this reference and click the “OK” button.
At this point you have your web service setup that is properly referenced from your Silverlight application.
Step 4. Making a call from your Silverlight application into your web service.
- In your Solution Explorer, from your web site open up MyService1.svc. By default there is one method made available called DoWork(). This doesn’t do much yet so let’s add a new function called HelloWorld() that will return a string. Make certain to include the [OperationContract] attribute in order to make this function visible:
[OperationContract]
public string HelloWorld()
{
// Add your operation implementation here
return "Hello World";
}
- In your Silverlight application project under your Service References, right click on ServiceReference1 and choose “Update Service Reference”.
- Next, let’s make a call from our Silverlight app to this HelloWorld() method. The following code which goes in Page.xaml.cs shows you how to make an async call to HelloWorld().
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
ServiceReference1.MyService1Client client = new SilverlightApplication9.ServiceReference1.MyService1Client();
client.HelloWorldCompleted += new EventHandler<SilverlightApplication9.ServiceReference1.HelloWorldCompletedEventArgs>(client_HelloWorldCompleted);
client.HelloWorldAsync();
}
void client_HelloWorldCompleted(object sender, SilverlightApplication9.ServiceReference1.HelloWorldCompletedEventArgs e)
{
string message = e.Result;
}
}
Note that all web services calls must be done asynchronously.
Thank you,
--Mike Snow
Subscribe in a reader