Developing Your First Silverlight Application Using Visual Studio 2008 - Consuming the ASP.NET Web Service from the Silverlight application using the Visual Studio 2008 solution
(Page 5 of 5 )
Once the web service and Silverlight projects are developed and configured as shown in previous sections, we need to link the Silverlight application with the ASP.NET web service, consume the service and present the consumed information on the Silverlight Canvas.
Go through the following steps to achieve the same:
Right click on the “SilverlightTest” project (in “Solution Explorer”) and click on “Add Web Reference.”
In the “Add Web Reference” dialog, click on “Web Services on the Local Machine.”
In the list of services, select “ProductService” by clicking on it (Fig 14).


Partial Public Class Page
Inherits Canvas
Public Sub Page_Loaded(ByVal o As Object, ByVal e As EventArgs)
' Required to initialize variables
InitializeComponent()
Dim svcNorthwind As New NorthwindService.ProductService
Dim objProject As NorthwindService.Product = svcNorthwind.GetProductInfo(2)
Dim txtProductName As New TextBlock
With txtProductName
.Text = "Total sale value of Product '" & objProject.ProductName & "' is " & objProject.SaleTillToDate
.Width = "437"
.Height = "25"
.SetValue(Canvas.LeftProperty, 8)
.SetValue(Canvas.TopProperty, 8)
End With
Me.Children.Add(txtProductName)
End Sub
End Class
Hit F5 to execute the application and point the URL to
http://localhost/SilverlightTest/TestPage.html
Pointing to the URL manually as above (in the browser) is essential as the request to the web service must be from the same web server as the Silverlight project. Otherwise, it may raise issues related to “cross domain access.”
Even though you are pointing manually to the localhost, the Silverlight project can still be debugged in the same traditional manner. The URL must be pointed to the browser window which came up when F5 is hit (otherwise, you will have to attach the process for debugging).
If everything went well, you should see the output as follows (Fig 16):

I hope you enjoyed the article and any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |