This is the second part of my three part blog introducing Silverlight Alpha 1.1.
This part will concentrate on getting “data” from a web service in the same domain. I will be enhancing the application I created in Part 1 of this series.
As a refresher this is where we left off with Part 1.
Now we’re going to be adding a website to this solution. This website will contain the web service, and in addition it will contain a page that will hold the XAML file.
Right click on the solution and then add a new project.
Create a Web Application. I called mine FirstWebServiceWeb.
The first thing we want to do in the web application is to add a “link” to the Silverlight project we have in the solution. To do that, right click on the web application project and you will see a “Add Silverlight Link…” menu option.
Once you have added the link you will see that some of the files from the Silverlight projects have been added to the web application. These files will be automatically updated every time the web application project is built.
Now let’s create our first web service. I’m calling my web service HelloWorldWS.asmx.
Once you have created the web service, make sure you comment in the line which allows the web service to be called from ASP.NET AJAX (see below.) If you don’t have a reference to System.Web.Extensions, you will get an error when you try to compile after commenting that line in.
Notice above that I also changed the text returned by the WebMethod so we know for sure the data is coming from service.
As I mentioned, this part of the blog is to use a web service which resides in the same domain as the Silverlight application. In order to facilitate the development, I like use the internal web server that comes with Visual Studio. So to make sure that I’m calling a web service that is in the same domain, I will be adding the root XAML (Page.xaml) to the Default.aspx page that was created. I’m going to add it by simply dragging the Page.xaml to the Default.apsx page in Design mode. In order for the drag and drop to work you need to make sure the Microsoft ASP.NET Futures (May 2007) are installed. Once the XAML was dropped I resized the object to the same size as the canvas in the XAML file.
To test the page, I’m going to make my web application the default project and the Default.aspx the Start Up Page. Then I’m going to run the application.
When you run the application you will see that you are getting an error indicating that you must defined a Script Manger on the page. The reason is that Silverlight uses ASP.NET AJAX to communicate with the server. I just dragged and dropped a script manager from the toolbox and when I run it again, I’m ready to go. You will find the exciting Silverlight application is now integrated with your ASPX page.
You can put other elements in the ASPX page you want. Note: in the Alpha 1.1 version of Silverlight, if you have multiple Silverlight applications on the same page each will run in its own sandbox and you will not be able to communicate between them.
OK, it is finally time to hookup the web service we created to the Silverlight application. To start, right click on the Silverlight project and click on the Add Web Reference… option. When the Web Service browser dialog appears click on the Web Services in this solution link. The next dialog will give you a list of all the services in the solution (at this time our only one). Choose that one and then click on the Add Reference button.
Just as in a regular managed code application when you add a reference using Visual Studio as set of files are created for you which have the references to the web service.
Now, we need to write some code to access the web service and populate our text block (see below)
The highlighted areas represent the new code I added. Notice that I’m assigning the Url to the HWS. That is not necessary; however, I wanted to point that out in case your internal web service changes port.
When you run your application by hitting F5 (remember the web application is the default project) you will see the data coming from the web service and populating the text block.
One of the beauties of the development of Silverlight applications in Visual Studio is how well the projects are integrated. You can place a debug stop on any line of code, either in the Silverlight application or the web application, and it will stop there.
That’s the end of this blog. My next blog will include calling a cross-domain web service to retrieve a set of photos for a gallery.