.NET Blog

Tony Cavaliere

 
My Favourite Albums
  And the Grappa wins.
E-mail me Send mail
Add to Technorati Favorites AddThis Feed Button

Subscribe to Cynot Why Not


Recent posts

Disclaimer

Hey unlike other bloggers I stand by what I say but just in case. The opinions expressed herein are my own except on Tuesday when the second card is not turned up otherwise it ain't worth squat.

© Copyright 2010

Headless Silverlight Application

A headless Silverlight application is a Silverlight application that contains no user interface. The obvious question is why, why on earth would you ever want to create a web site with a Silverlight control that has no UI? I can think of good reason. What if you want to make a cross domain call. Traditionally, this was done by making a call back to the same domain server. The server would make the requested call on behalf of the client and return the data back to the client. Wouldn't it be great if the client could make the call directly.

Silverlight has the capability of making cross domain calls provided the server that hosts the service has been given access via the either the ClientAccessPolicy.xml or crossdomain.xml file. This post will describe how to use a headless Silverlight control to simplify cross domain calls from the client browser. I will not spend too much time discussing how to create a WCF service but rather will use an existing WCF service. The reader is directed to a previous post Calling a WCF Service from Silverlight 2.0: part Two. Likewise, I will not be discussing, in any great detail, how interoperability between Silverlight and the HTML DOM is done. Again the reader is encouraged to read a prior post  How to communicate between Silverlight controls.

Let's start writing this headless Silverlight application.

Using VS2008, create a new Silverlight application. I decided to call the project HeadlessSilverlight. I like to use jQuery and if you want to follow this tutorial, as is, then I recommend you add jQuery.js to the web project. You can download jQuery from jquery.com. I also like to remove the generated default.apsx file and rename the generated TestPages to default. So the HeadlessSilverlightTestPage.aspx is renamed to default.apsx and the HeadlessSilverlightTestPage.html to default.html. After doing all this you should have a solution similar to Figure 1.

Figure 1: Initial Solution

Solution1

The next step to is alter the Silverlight control so that it has no height or width. Edit the default.aspx and make the following changes:

  1. Remove the width and height in the Silverlight control.
  2. Remove the style attribute from the div element that hosts the Silverlight control.

Listing 1 contains the altered markup.

Listing 1: The Silverlight control with no height or width.

   41         <div>

   42             <asp:Silverlight

   43               ID="Xaml1" runat="server"

   44               Source="~/ClientBin/HeadlessSilverlight.xap"

   45               MinimumVersion="2.0.31005.0" />

   46         </div>

Similarly the XAML needs to be modified. The modifications are shown in Listing 2.

Listing 2: headless Silverlight XAML markup.

    1 <UserControl x:Class="HeadlessSilverlight.Page"

    2    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    3    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    4    >

    5     <Grid x:Name="LayoutRoot"></Grid>

    6 </UserControl>

Of note both the grid and the user control have no width or height.

We need some way to initiate the cross domain service call. Modify the default.apsx markup to include a button. When the user clicks this button we will call the service but rather than use javascript to directly call the service we will use Silverlight to accomplish the task. Listing 3 contains the markup for the button. Place this after the markup for the Silverlight control.

Listing 3: HTML Button  

   47         <div>

   48           <input

   49             id="btnCrossDomain" type="button"

   50             value="Call Cross Domain Service" />

   51         </div>

Notice that we have not wired up the click event in markup and this is where jQuery shines. jQuery makes it easy to decouple the UI from behaviour. Enough said let's see how the click is wired up.

Listing 4 contains the javascript call to jQuery that wires up the click event to an anonymous function (sometimes called inline function).

Listing 4: Supporting javascript code.

   20     <script type="text/javascript" src="jquery.js"></script>

   21 

   22     <script type="text/javascript">

   23       // Use jQuery to attach a function to the button click event

   24       $(function() {

   25         $('#btnCrossDomain')[0].onclick = function(event) {

   26           //call service via silverlight

   27           var host = document.getElementById("Xaml1");

   28           host.content.SL_Headless.CallService();

   29         }

   30       });

   31       // The Silverlight managed code will call this javascript function

   32       // when it has made the service call.

   33       function displayResponse(response) {

   34         alert(response);

   35       }

   36     </script>

First we need to include the jQuery library. This is done at line 20. Lines 24 to 30 is basically how we wire up the click event of the button with id #btnCrossDomain with an anonymous function. This anonymous function makes the call into the Silverlight managed environment and ultimately makes the service call. More on that later. The function displayResponse will be called from the Silverlight managed environment and will display the data from the service call. More on that later.

Let's examine more closely how the call to Silverlight is made. Line 27 is used to retrieve the Silverlight plug-in instance. This instance exposes a content property  from which you can access a scriptable object. Only managed instances that have been explicitly registered are accessible from javascript. Once we have a reference to the scriptable object then any method that has been decorated with the ScriptableMemberAttribute can be called. This can be a little confusing and will become clearer when we investigate the Silverlight code.

But before we can add the code that actually calls the WCF service, we need to add a service reference to the Silverlight project. Select the Silverlight project and press the right mouse button and select the Add Service Reference menu option. This will bring up the Add Service reference dialog. I have decided to use a service I created for an earlier post. You are free to use this service or any other but remember if you chose a third party service then make sure that they have granted access to the service via the ClientAccessPolicy.xml file. Type the following in the Address field of the dialog http://www.cynotwhynot.com/services/people/PersonService.svc  and then select Go. After a bit of time the Services listbox should contain a service named PersonService. Lastly, change the namespace to PersonProxy. Figure 2 shows this dialog.

Figure 2: Adding a reference to the WCF service.

Add Service  

Selecting the OK button will add the proxy to the Silverlight project. This service contains a single method, GetPeopleAsync and is used to get a list of Person objects.

With this reference in place we can concentrate on the code behind for the headless Silverlight control. Listing 5 contains the code for Page.xaml.vb

Listing 5: The Page.xaml.vb code behind.

    1 

    2 Imports System.Windows.Browser

    3 Imports System.Collections.ObjectModel

    4 

    5 Partial Public Class Page

    6     Inherits UserControl

    7 

    8     Public Sub New()

    9         InitializeComponent()

   10     End Sub

   11 

   12     <ScriptableMember()> _

   13     Public Sub CallService()

   14         Dim proxy As New PersonProxy.PersonServiceClient()

   15         AddHandler proxy.GetPeopleCompleted, AddressOf onGetPeopleCompleted

   16         proxy.GetPeopleAsync()

   17     End Sub

   18 

   19     Public Sub onGetPeopleCompleted( _

   20         ByVal sender As Object, _

   21         ByVal e As PersonProxy.GetPeopleCompletedEventArgs)

   22 

   23         Dim response As String

   24         If e.Error Is Nothing Then

   25             response = String.Format( _

   26                 "Cool a cross domain call. {0} {1}", _

   27                 e.Result(0).First, e.Result(0).Last)

   28         Else

   29             response = String.Format( _

   30                 "Cool a cross domain call but an error occurred. {0}", _

   31                 e.Error.ToString)

   32         End If

   33 

   34         'Call Javascript to display the response

   35         Dim displayResponse As ScriptObject = CType(HtmlPage.Window.GetProperty("displayResponse"), ScriptObject)

   36         displayResponse.InvokeSelf(response)

   37 

   38     End Sub

   39 

   40 

   41     Private Sub Page_Loaded( _

   42         ByVal sender As Object, _

   43         ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

   44         HtmlPage.RegisterScriptableObject("SL_Headless", Me)

   45     End Sub

   46 

   47 End Class

Lines 12 through 17 contains the code that makes the call to the WCF service. The ScriptableMemberAttribute on line 12  and the call to RegisterScriptableObject on line 44 exposes the CallService method to javascript.  The call to the WCF service is made within the CallService method. The Silverlight model enforces that all out of band calls must be made asynchronously and therefore the response must be processed within a callback, OnGetPeopleCompleted. This callback takes the return data (e.Results) and packages it into a string. For simplicity only the first person in the list is included in the response string. If a problem occurs when calling the service, the error is placed in the response string. Finally, lines 35 and 36 call the javascript function, displayResponse and if you recall this function calls alert, displaying the response string .

Figure 3 shows the alert containing the data from the WCF service call. Notice that the alert contains the person John Smith. That's it a cross domain call from within the client browser.

Figure 3: The headless Silverlight application running in a browser.

headless app running 

Without your space helmet, Dave, you're going to find that rather difficult.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Silverlight
Posted by CynotWhyNot on Friday, November 28, 2008 4:55 PM
Permalink | Comments (21) | Post RSSRSS comment feed

Related posts

Comments

answerspluto.com

Monday, July 13, 2009 10:09 PM

pingback

Pingback from answerspluto.com

list of urls - 5 « Answers Pluto

debt collection agency us

Thursday, September 10, 2009 6:00 AM

debt collection agency

Now this is hghly recommeded post for me. I will surely email this to my friend.


Regards

Loop

Translation Services us

Monday, September 14, 2009 3:42 AM

Translation Services

I loved the way you exlained things. Much better many here


Regards

Nimi

Polywood lawn furniture us

Monday, September 14, 2009 6:01 AM

Polywood lawn furniture

Wonder full writing skills you got mate.


Regards
Diva

discount designer bag US

Tuesday, December 01, 2009 7:39 PM

discount designer bag

I find your blog in google. And I will be back next time, thanks.

swimming pools construction us

Monday, December 14, 2009 2:40 AM

swimming pools construction

Really fascinating subject, I will bookmark your website to check if you write more in the future. Kind regards,




Regards
Lewis

air force 1s US

Thursday, December 24, 2009 3:52 PM

air force 1s

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

discount designer handbags US

Wednesday, December 30, 2009 6:55 AM

discount designer handbags

If you need to buy discount gucci bags, you can just type bagsvendor.com in you browser address line and find out the gucci bags on big sale in discounted price. The information below is only introducing the discount news about gucci bags and gucci handbags. A woman leads the world accounting her gucci bag. But then we must also remember that there is a well discount news that has to see. It is a common complaint that women having their share is not large enough to hold all your belongings necessary, such as discount gucci bags, designer bags. This gucci bags you can find in bagsvendor.com is that the person is unable to organize things properly. Each discount bag has a clear purpose and objects you choose to take the bag must be compatible with the purposes for which it is purchased in your mind. We can meet the wide range of needs that are part of the common things for a trip on a beautiful handbag, bags or purses. Thus, while the discounted gucci bags needs, here are some points that may be useful to organize the contents of the discount gucci bag. Even it is a discounted price gucci bags, but it has high quality. The quality of materials used with the gucci bag is leather, suede or cotton. These materials can be sure that they are not less than 100% authentic and of excellent quality discount bags. Accessories and decorative items from the discount gucci bag, such as hinges, chains, rings and belt buckles are higher that other bags. Some designers in the Gucci company will even give as a repair or replacement free of charge in a period of time to ensure that their discount handbags are offered in a different league than the shares that you can find at any mall or online store. Designer Gucci bags can be changed to discounted gucci bags. This is a big news about handbags on sale. Brands are just called by many famous fashion people, and can be done with any discounted news. Just find more in bagsvendor.com

air jordan US

Friday, January 01, 2010 2:51 AM

air jordan

In winnersneaker, these Nike Air Jordan shoes were associated with sport, fashion and basketball. The Nike Air Jordan sneakers were a way to hide some of the smaller sizes, or raise an important NBA star to even higher levels to the count. Nike Air Jordan shoes, but also less fashionable of Air Jordan history. Which were commonly used by high-ranking customers in NBA during the 19th century and were used in the 20th century to let the putrid mud in the streets, all you know is that you can find many discounted Air Jordan shoes in winnersneaker.com. Existing Air Jordan retro sneakers in the United States and Europe retails do not feel in fashion until the new Air Jordan shoes come in the street. At first the Air Jordan 1 shoes important wearing for young sport men, but once reigned basketball, Jordan shoes became the must-have fashion accessory for young people in the basketball playground. These Air Jordan original shoes were all to make a statement vividly. Air Jordans like Air Jordan 23 and Air Jordan 6 wore basketball shoes kiss in the context of his people larger than life. Wish you find a suitable Air Jordan shoes in winnersneaker.com.

Oil Change us

Tuesday, January 26, 2010 2:32 PM

Oil Change

if every editor wrote like you believe me the world would be a better place! this was an excellent read expecting more!

lose weight us

Friday, February 19, 2010 11:27 AM

lose weight

One worthwhile task carried to a successful conclusion is worth half-a-hundred half-finished tasks.

discount designer bags US

Sunday, March 07, 2010 4:04 PM

discount designer bags

Nice content, I trust this is a nice blog. Wish to see fresh content next time.

Merrill au

Monday, March 08, 2010 3:30 PM

Merrill

I have been experimenting with the express editions of Visual Studio to build my knowledge for a number of months and I am beginning to undertake the conversion to the professional edition. Before I do this I would like to extend my detailed knowledge of using the package before I spend real money. My previous experience is as a web coder with mysql, php, most linux based tools and a little flash with actionscript, now I am moving to a Microsoft package. I am finding it is a real learning curve and am attempting to build my awareness with some specialist blog reading. The phrase " CSS intellisense with Visual Studio 2008 " in your post got me aroused my curiosity. I find the Visual Studio tutorial sites are often inflexible and mention the identical items looking like an online manual. Comments and dialog in web logs can cover effective ways to beat problems that leads me through the learning curve more quickly. Thank you for the note.

discount designer bags sale US

Monday, March 08, 2010 10:54 PM

discount designer bags sale

Yeah, you are right, I agree with you.

discount designer handbag US

Tuesday, March 09, 2010 2:33 PM

discount designer handbag

Your blog seems interesting.Regards,Kevin.

james shoes sale US

Wednesday, March 10, 2010 9:44 PM

james shoes sale

I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people.

worldwide shoes china US

Wednesday, March 10, 2010 11:27 PM

worldwide shoes china

Amazing article. Bookmarked it already. Best regards, Ken.

discount designer bags sale US

Thursday, March 11, 2010 2:47 AM

discount designer bags sale

Easily, the post is really the greatest on this laudable topic. I concur with your conclusions and will thirstily look forward to your future updates. Saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay privy of any updates. Solid work and much success in your business enterprize!

nba basketball shoes US

Thursday, March 11, 2010 6:47 AM

nba basketball shoes

What a great info, thank you for sharing. this will help me so much in my learning.

china shoes US

Friday, March 12, 2010 12:05 AM

china shoes

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

shoes china US

Friday, March 12, 2010 2:50 PM

shoes china

You have a point. Very insightful. A nice different perspective

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Friday, March 12, 2010 4:39 PM