How To: Call a Java EE Web Service from a .Net Client
How To: Call a Java EE Web Service from a .Net Client
Many organizations have server side investments in Java technologies. While they want to build a compelling UI with Microsoft’s latest technologies, such as WPF and Silverlight, they still want to benefit from those existing investments instead of rewriting them. In order to do so, we have to bridge between those technologies and allow client side technologies consume Java web services.
This post is a step by step guide for building a Java EE Web Service, and a .Net client application that consumes it.
Before we get started with this walkthrough, make sure you have the following installed on your machine:
Create a Java Web Service (Java EE, JAX-WS)
1. Create a new Web Application
In the NetBeans 6.1 IDE, choose File –> New Project. In the New Project Dialog select theWeb category, and choose Web Application from the projects list. Then, Click Next.
* If the web category is not available in this dialog, it means that the NetBeans version you have installed isn’t the Web and Java EE package.
In the Name and Location page, set the location where you want to create the web application, and provide a name for the project. Click Next.
In the Server and Settings page, leave the default settings (Java EE 5, Use GlassFish V2) and Click Finish.
This creates the initial web application and opens the project for editing.
2. Create the Web Service
Add a new web service to the project. Right click the project node and choose New –> Web Service.
* Notice that the location of the Web Service option in the menu may change from this image and your IDE.
In the New Web Service dialog, provide the Web Service Name, and the Package. The name of the service will affect the final URL for calling the service, and the package name will be the namespace of the service contract. Click Finish.
The Web Service now appears in the project tree.
To implement the service, double click the service node in the project tree (in the figure above – CalculatorService). This will open the Web Service in Design mode, that lets you graphically design your service.
Change to Source View by clicking on the Source button in the upper toolbar, and this will open the CalculatorService.Java file for editing. Here is a sample implementation of the service. Notice how Java Annotations are similar to .Net Attributes, especially how similar they are to the Web Services attributes we know…
package org.bursteg.calculator;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public class CalculatorService
{
@WebMethod
public int Add(@WebParam(name="a") int a,
@WebParam(name="b") int b)
{
return a + b;
}
}
Deploy the web service to the web application server. From the NetBeans IDE this is done byright clicking the project node, and choosing Undeploy and Deploy.
After the web application has been deployed, just to make sure the web service works as expected, you can right click the web service node, and choose Test Web Service.
This will open the browser and navigate to a test page with the url of the service (http://localhost:9232/Calculator/CalculatorServiceService) with a ?Tester suffix.
Call the Java Web Service from a .Net Client
In Visual Studio 2008, create a new console application.
This creates a new solution with a single Console Application project in it.
Right click the project node and choose Add Service Reference.
In the Add Service Reference Dialog, paste the address of the service metadata endpoint (service address + ?wsdl suffix: http://localhost:9232/Calculator/CalculatorServiceService?wsdl), and click Go. The dialog will get the service metadata and understand the service contract.
Provide a namespace for the service reference, and click OK.
This will generate the client side proxy that lets you consume the service easily, and the required configuration settings into the configuration file.
To call the service using the generated client side proxy, open Program.cs and use the following code:
static void Main(string[] args)
{
CalculatorServiceClient proxy = new CalculatorServiceClient();
int result = proxy.Add(2, 3);
Console.WriteLine("Calculator Service returned: " + result.ToString());
}
Run the program and see that the web service is being called and the result is correct.
Conclusion
Since Java EE Web Services (JAX-WS) are standard SOAP services, they are easily interoperable from a .Net client application with only several clicks. Visual Studio generated a.Net client proxy that makes it very easy to connect and call the service.
Enjoy!