Wednesday, February 18, 2009

Interoperability – F# + Silverlight 2 on Apache + Metro Web Services on Tomcat

I bought the books Silverlight 2 in Action and Pro Silverlight 2 in C# 2008 for a while now and did not have a chance to work with Silverlight for the past several weeks.  However, recently I had to investigate using GlassFish’s Metro library to build web services as it was highly recommended by colleagues.  I have previous familiarity with using Apache Axis to build web services as that web services stack had strong plugin support with Eclipse, my IDE of choice when it comes to Java development.  I thought this was a great opportunity to build an sample web service using Metro and build the web service client on Silverlight platform.

My sample web service code was intentionally simple and the java code is as follows:


import javax.jws.WebService;
@WebService public class HelloWorldService {
public String hello(String name) { return "Hello, " + name;


I followed the instructions on the web to create the applicationContext.xml and web.xml file and deployed it to my Tomcat server.  However, I had a hard time getting this simple example to work.  In the end, I downloaded Netbeans and created the web service solution in that IDE.  Netbeans generated the applicationContext.xml and web.xml file for me and it worked like a charm.  Once I have those configuration files, I could easily do subsequent changes in Eclipse without any problems.  This demonstrated how tool support can make life a lot easier for developers.  Once the initial setup is completed, it’s much simpler to build and modify from a working solution.  Here are the configuration files for those who might be interested:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<wss:binding url="/helloworld">
<ws:service bean="#HelloWorldService">
<ref bean="#HelloWorldService">

<!-- this bean implements web service methods -->
<bean id="HelloWorldService" class="" />


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">
<!-- this is for Spring -->
<!-- these are for JAX-WS -->

Similarly, on the Silverlight side…I needed the tools support in Silverlight on Visual Studio 2008 to help me generate the Service Reference library.  I opened up Visual Studio 2008, right-click my Silverlight library project and chose Add Service Reference… and then put in the url of my web service, which in my case was hosted at http://localhost:8080/metropilot/ws/hello?wsdl.  I would then build it as a Silverlight library that I can reference in the F# project.  I assume and hope that when Visual Studio 2010 arrives, I can do all of this without leaving the F# environment.  Now I can write my F# Silverlight client which asks the user to type in a name that will be sent to the web service and display the returned greeting.  The F# client code is as follows:

namespace SilverLightFSharp

open System
open System.Windows
open System.Windows.Controls
open System.ServiceModel
open List

// Data Binding example
type MyPage = class
inherit UserControl

new () as this = {} then

let sp = new StackPanel()

let textblock = new TextBlock()
textblock.Text <- "Please enter your name:"

let textbox = new TextBox()

let button = new Button(Content="Get Greeting...")

button.Click.Add(fun e ->
let binding = new BasicHttpBinding();
let endpoint = new EndpointAddress("http://localhost:8080/metropilot/ws/hello?wsdl");
let service = new ServiceLibrary.MetroPilotService.HelloWorldServiceClient(binding,endpoint)
service.helloCompleted.Add(fun e ->
textblock.Text <- e.Result.ToString()

this.Width <- 400.0
this.Height <- 300.0
this.Content <- sp

type MyApp = class
inherit Application

new () as this = {} then
this.Startup.Add(fun _ -> this.RootVisual <- new MyPage())
//base.Exit.Add( fun _ -> ()) //this.Application_Exit)

My application.xap file has the following files:

  • AppManifest.xaml
  • FSharp.Core.dll
  • ServiceLibrary.dll – this was the library generated using Visual Studio 2008 via Add Reference…
  • SilverlightFSharp.dll – the compiled client code

Since I’m running this Silverlight client on an Apache web server that’s running on port 8000 and my web services was running on a Tomcat server on port 8080, I had to create the clientaccesspolicy.xml and crossdomain.xml file and put it in the root folder of the Tomcat server or the Silverlight client would refuse to access the web service on my Tomcat server. 

My clientaccesspolicy.xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<allow-from http-request-headers="*">
<domain uri="*"/>
<resource path="/" include-subpaths="true"/>

My crossdomain.xml file is as follows:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "">
<allow-http-request-headers-from domain="*" headers="*"/>

Finally, after all these configurations, I got my F# Silverlight 2 client code running on Apache web server talking to my Java based web services running on Tomcat.

1 comment:

appdynamics integration said...

Thanks for sharing the real crakers to handle projet stuff in an intelligent and cohesive way !