Pick a Silverlight RIA scenario [D2D, B2E, B2B, B2C] or choose a Web Services style [SOAP/RPC, XML/REST]
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
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
Host the Silverlight application in an Eclipse project running on Tomcat
<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 += "MethodName: " + args.methodName + " \n";
throw new Error(errMsg);
</script>
<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>
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