The HowTo serie demoes Silverlight accessing Web Services in various configuration. In this post, we will demonstrate a REST service consumed by a Silverlight client application where the Site of Origin hosts the Web Services. This scenario is categorized as a D2D scenario without cross-domain policy.

The XAP package, the REST service Eclipse project and the Silverlight Eclipse project are available as an attachment to this post.

Technical Environment

  • REST services are built with the Restlet framework and hosted in Tomcat
  • The Silverlight application is also hosted in Tomcat

image 

Because the Site Of Origin and the Web Services reside in the same host, no Cross Domain policy is required.

To successfully install the technical environment, please take the following step :

If you don’t feel like doing the previous step, the XAP package available as an attachment contains a Silverlight client which is configured to access a REST service at http://localhost:8080/Restlet-servletfirstResource/items.

The REST service is also available as an attachment to this post allows to do a GET and POST method to get a list of items or to insert a new one.

Download the attachment and unzip it. There are two needed parts : the Silverlight XAP package and the Restlet Eclipse project.

 

Host the REST service with Tomcat

  • Import the Reslet-servletfirstResource project into your Eclipse workspace :
    • go to File –> Import
      image 
    • Then chose General—>Existing Projects into Workspace
      image 
    • Select the root directory, Browse to the Restlet-servletfirstResource folder, then click on Finish. The Restlet-servletfirstResource has now been imported into your workspace
      image
  • Run the project on Tomcat Server. check that http://localhost:8080/Restlet-servletfirstResource/items is accessible you should get the following result :
    image 

Host the Silverlight application in an Eclipse project running on Tomcat

  • Create a new Dynamic Web Project
    • Press Ctrl+n to go to the New Wizard Project, under Web select Dynamic Web Project.
      image
    • Call it REST-SilverlightClient and ensure that the Target Runtime is set to Apache Tomcat 6.0
      image
    • If Eclipse prompts you to switch to J2EE perspective, accept it.
  • In the REST-SilverlightClient project, create the WebContent\ClientBin folder and copy the XAP package into it.
  • Now we need to host it through an Active X control :
    • Right click on WebContent folder and select New—>JSP, call it index
    • Edit index.jsp, in the Head part put the following script that catches Silverlight Error.
 <script type="text/javascript">
     function onSilverlightError(sender, args) {
     
         var appSource = "";
         if (sender != null && sender != 0) {
             appSource = sender.getHost().Source;
         } 
         var errorType = args.ErrorType;
         var iErrorCode = args.ErrorCode;
         
         var errMsg = "Unhandled Error in Silverlight 2 Application " +  appSource + "\n" ;
 
         errMsg += "Code: "+ iErrorCode + "    \n";
         errMsg += "Category: " + errorType + "       \n";
         errMsg += "Message: " + args.ErrorMessage + "     \n";
 
         if (errorType == "ParserError")
         {
             errMsg += "File: " + args.xamlFile + "     \n";
             errMsg += "Line: " + args.lineNumber + "     \n";
             errMsg += "Position: " + args.charPosition + "     \n";
         }
         else if (errorType == "RuntimeError")
         {           
             if (args.lineNumber != 0)
             {
                 errMsg += "Line: " + args.lineNumber + "     \n";
                 errMsg += "Position: " +  args.charPosition + "     \n";
             }
             errMsg += "MethodName: " + args.methodName + "     \n";
         }
 
         throw new Error(errMsg);
     }
 </script>

 

    • In the Body part, instantiate a Silverlight control by adding the following HTML code, ensure that the param named “source” has is value set to the XAP package URI relative to index.jsp Web Page:
<div id="silverlightControlHost">
    <object data="data:application/x-silverlight," 
            type="application/x-silverlight-2-b2" 
            width="100%" height="100%">
        
        <param     name="source" value="ClientBin/REST-JavaServiceItem.xap"/>        
        <param     name="onerror" value="onSilverlightError" />
        <param     name="background" value="white" />
    
        <a     href="http://go.microsoft.com/fwlink/?LinkID=115261" 
            style="text-decoration: none;">
               <img src="http://go.microsoft.com/fwlink/?LinkId=108181" 
                 alt="Get Microsoft Silverlight" 
                 style="border-style: none"/>
        </a>
    </object>
    <iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>
</div>
  • Run the project on Tomcat Server and the Silverlight control will appear in the Eclipse Web Browser :
    image

The great thing about that is that you will be able to test your REST Service and the Silverlight application without running multiple Development Environments.

- Ronny Kwon