Monday, January 3, 2011

Grails,CXF plugin and .NET

As promised here's a follow up on the WebService interoperability between Grails using Apache CXF plugin and .NET.

First let's create a simple example project and install the grails-cxf plugin:
grails create-app example
grails install-plugin cxf

Next let's create a service:
grails create-service example

This has created 2 files:
grails-app/services/example/ExampleService.groovy
and
test/unit/example/ExampleServiceTests.groovy

The first one is the one we need to modify so let's get to it:
package example

import javax.jws.*

@WebService(serviceName="ExampleService", name="Example")
class ExampleService {

static expose = [ 'cxfjax' ]

@WebMethod(operationName="sayHello")
String sayHello(@WebParam(name="name") String name) {
"Hello ${name}!"
}
}

As you can see I've added every possible bit of information to customize the generated WSDL. With all that in place we can get the description of this service (WSDL) at http://localhost:8080/example/services/example?wsdl. Let's use that to create a .NET client. This time from command-line:
SET PATH=%PATH%;"C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin"
wsdl.exe http://localhost:8080/example/services/example?wsdl

In the first line we're expanding the system path so that all the tools are available from anywhere. In the second line we're generating a client for the web service we've created in Grails. Simple enough, right?

Let's create a simple console application and use this newly generated client:
Program.cs:
using System;

public class Progra {
public static void Main(String[] args) {
Console.WriteLine(new ExampleService().sayHello("John"));
}
}

Let's compile everything from command-line:
set PATH=%PATH%;C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319
csc *.cs

Again, no magic here. Once you've compiled everything run the application and bum! Everything works as expected :)
My guess is that CXF plugin didn't work just right out of the box because of some problems with default naming that CXF is using when auto-generating WSDL. With all the annotations in place everything seems to be working just fine.

I hope this will help someone. In case you'd like to fiddle with it yourself here's the example. in src/csharp you'll find the sources for the client along with a batch file to compile them.

Unfortunately when it comes to a more advanced scenario, like for example returning an array of domain objects from a service method the CXF plugin still fails to produce good enough WSDL to work with .NET. This is the case where XFire plugin shines best!

FYI: the same trick does not work with Axis2 plugin. The naming is all whacko. Things like this$dist$set$2 and urn:this$dist$get$2 hurt the WSDL code generator so much it spits nothing out but a load of warnings.

Have fun!

Sunday, January 2, 2011

JavaEE 6 + Metro WebServices

Following the post about WebServices with Grails here's a short summary of my attempt to make Java's EE 6 WebServices work with .NET. It's quite a basic example but as history shows even the simplest examples might not work (as it is the case with Axis2).

Setup

So here's a definition of a Hello WebService:
package com.aplaline.integration.webservices;

import javax.jws.*;

@WebService(name="Hello", serviceName="HelloService")
public class HelloService {
@WebMethod
public String sayHello(@WebParam(name="name") String name) {
return "Hello " + name + "!";
}
}

As you can see there's no magic here. Let's see how NetBeans' configuration option look like for this guy:



Surprisingly enough there's mentioning of a .NET version! Let's see how this works. I'll be using VWD 2008 SP1 to test it:



Let's add a web reference to this web site:



Now let's create a blank Default.aspx page and add some code to call that web service:
Default.aspx:
[...]
<form id="form1" runat="server">
<div>
<table>
<tr>
<td><asp:Label ID="Label1" runat="server" Text="Enter name" /></td>
<td><asp:TextBox ID="input" runat="server" /></td>
</tr>
<tr>
<td align="right" colspan="2"><asp:Button ID="Button1" runat="server"
onclick="Button1_Click" Text="Click me" /></td>
</tr>
<tr>
<td colspan="2"><asp:Label ID="output" runat="server" /></td>
</tr>
</table>
</div>
</form>
[...]
Default.aspx.cs:
[...]
protected void Button1_Click(object sender, EventArgs e)
{
output.Text = new localhost.HelloService().sayHello(input.Text);
}
[...]

Results

As it turns out it works out of the box properly.



For the kicks of it let's try and consume the same web service from PHP 5.3:
<?php
$client = new SoapClient("http://localhost:8080/WebServices/HelloService?wsdl");
var_dump($client->sayHello(array("name" => "John")));
>
There's a little inconvenience when calling web services from PHP in that the parameters need to be passed as an array instead of simply specifying their value. In the long run it's kind of healthy because different webservice stacks tend to handle things differently and putting the proper context into the whole thing.



This is the first pleasant surprise since I've started to work on the web-service interoperability between .NET and Java. It looks like things are definitely going in the right direction :)

Here's the Java project for reference.
Here's the .NET website for reference.

Have fun!

Friday, December 31, 2010

Grails, WebServices and .NET interoperability

This is just a quick note for all those that have tried to interop between Grails and .NET using Apache CXF or Apache Axis2 and failed miserably:

Don't use those libraries - instead use the good old grails-xfire plugin. This guy just works out of the box with .NET 3.5 and 4.0. In C# just add a web reference and you're all set!

More on this topic soon - stay tuned!

Thursday, December 30, 2010

Running PHP applications on .NET

Continuing my good fortune in integration between completely different platforms I've tried to integrate some PHP interpreter/compiler called Phalanger 2.0 with existing PHP applications and run them on .NET using VWD server.

PhpBB 3.x



Well, it was no easy thing to do but I've got it to work. And let me tell you it works fast!

One thing to note is that PhpBB did not like the included MySQL connector and I've had to download this one and install it instead.

WordPress



Well, after I've gotten PhpBB to work using the right web.config getting this guy to work was just a matter of minutes.

However getting it to display anything is a nightmare on the internal VWD server because it does not support default documents. So everytime I wanted to display a page I've had to append some stupid parameters to the URL so that the part "index.php" would not get lost.



Others (phpMyAdmin, MediaWiki)

The docs here state that Phalanger 2.0 is perfectly capable of running those 2 applications but as practice has shown there was absolutely no way to bend them to .NET environment. Pity...

I'm going to check how things are with pure IIS and its capabilities for defining the default document :)

Stay tuned!

Compiling Groovy 1.7.6 with IKVM

If you'd like to see how to compile Groovy 1.7.6 under IKVM.NET take a look at this archive. It contains everything you need to be able to turn the groovy-all-1.7.6.jar (and its dependencies) into both an executable (that runs the console) and a library that can then be embedded into a .NET application.

Beware: This is a 27MB download so it's not a small thing.

groovy-1.7.6-net.zip

A note about the compilation process

Please note that there are some issues during 2 phases of the compilation:
1. The first one occurs when compiling commons-logging-1.1.1.jar as it also requires LogKit which I was to lazy to collect and compile
2. The second one occurs while compiling Groovy itself as there are still some dependencies missing. They aren't however important enough to block the usage of compiled library and executable.

Let me know if you'll find this interesting and/or useful :)

Groovy scripting from C#

Well, after the last finding that the Groovy Console executes on IKVM.NET what must have followed was embedding of Groovy scripting capabilities in a C# application.



Here you can snatch the project along with everything needed to compile and run it (besides the .NET framework of course). Beware, it's a 19MB download (50MB+ after unzipping). I've created it using Visual C# Express 2010.

Have fun!

Groovy 1.7.6 under .NET

Well, I finally did it! It did require a huge leap of fate but it finally turned out possible: Groovy running on .NET using IKVM 0.44.0.5!



And not only running but also interoperating with .NET classes!



It's running extremely slow but stable enough to play around a bit with it.

Next step: using Groovy classes from .NET!