Tuesday, August 18, 2009

Consuming WCF service in Java

This post explains consumption of a WCF service in Java client.
Let us first create a WCF service. Open your Visual studio 2008 and create a WCF service. Visual studio will create a default service for you and it would look like the below code.

To make it interop I will pick basicHttpBinding. Please note that to make it interoperable either we can choose "basicHttpBinding" or "wsHttpBinding".


Below is the service method.

public class Service1 : IService1
{
public string GetWelcomeMessage(string value)
{
return "Welcome to WCF " + value;
}
}

Note: You can use wsHttpBinding also, but wsHttpBinding is not supported by all Java IDEs. You have to use something like Axis2 IDE.

Once we are done with the development of the service. You can know about how to host in IIS @
Hosting WCF service in IIS.

Since I need to host it in IIS i need to make the changes in my web.config as app.config file. Below is the web.config file. It is the same as app.config file



Now we are done with our WCF Service. Now lets say that this service is hosted in "ABC123" machine (typicall on your machine "ABC123" would be "localhost").

Let us build the Java client for this. We will use Net Beans IDE 5.5 for this example. We will name the application as "javaapplication1". Below are the pre-requisite for the NetBeans application

• WCF service should be hosted in IIS
• NetBeans IDE for Java
• In-built Application server – SunOne/Apache Tomcat
• JDK 1.5 or higher
• JARs :- JAX-RPC packaged Jars

First step is to add a web service client. Right click on "Source Packages" of your appliation. Select "New" and then "Web Service Client". It would look like



Enter the WSDL location of the service in the "proxy host" text box. It would look something like this.This address would contain the host address "ABC123" in our case



Let say we have a simple Frame with a text box. The user has to enter some input value in the text box and click the Consume button. The click Invokes the WCF service hosted on "ABC123" machine, passes the input and receives the output, post execution of the service and displays the same in the output textbox. The Frame would look like this



The handler of the Button Click has to be coded like:-

String input = jTextField1.getText ();
try {
javaapplication1.Service1 service1 = new javaapplication1.Service1_Impl();
javaapplication1.IService1 basicHttpBinding_IService1 = service1.getBasicHttpBinding_IService1();
jTextField2.setText (basicHttpBinding_IService1.getWelcomeMessage (input));
} catch (javax.xml.rpc.ServiceException ex) {
} catch (java.rmi.RemoteException ex) {
} catch (Exception ex) {
// TODO handle custom exceptions here
}

The output would look like



I hope this help to some one who wants to consume WCF in JAVA.

2 comments:

Anonymous said...

Nice Atricle.. Thank you

Anonymous said...

Nice but what about using Secure WCF service consumed in Java. I am trying to do but no success yet.

May be you can give some hints