Java client and WCF server

Apr 23, 2009

This article is a part of WCF & Java Interop

WCF’s fundamental communication mechanism is SOAP-based Web services. Because WCF implements Web services technologies defined by the WS-* specifications, other software which is based on SOAP and supports WS-* specifications can communicate with the WCF-based applications.

To build up a cross-platform WCF server, you can use Metro. Metro is a Web Services framework that provides tools and infrastructure to develop Web Services solutions for the end users and middleware developers. It depends on Java programming language. The latest version of Metro is 1.4.

In the development process of SCM Anywhere (a SCM tool, with fully integrated Version Control, Issue Tracking and Build Automation, developed with WCF and METRO/WSIT), we found that METRO is NOT as mature as WCF. There are lots of small issues in METRO/WSIT. Luckily, METRO is an open source project and keeps evolving all the time. Our experience is that if you find some features are not working properly in METRO, keep downloading the latest version from Several weeks later, you may discover that the features are working properly.

To implement a Java client to communicate with the WCF server, you can follow the steps below:

  1. Download METRO/WSIT from the home page of Metro:

  2. Download Eclipse. We use Eclipse + Metro to develop Dynamsoft SCM Anywhere.

  3. Install Metro by executing the command: java –jar metro-1_4.jar. The installation folder of Metro contains some documents, tools and samples. You can find the documents in the “docs” folder.

  4. Use the C# project “WcfService1” (provided in my WCF client and WCF service article) as the WCF server. Go to Property of the WCF project, and set the server port to one that is not occupied by other services. Here we used 8888 for example. In the “web.config” file, change the string “wsHttpBinding” to “basicHttpBinding”.

  1. This is the key step. We use the wsimport tool included in Metro to generate the Java client code. Create a file named “service1.xml” and copy the following code to the file:

<bindings xmlns:xsd="" xmlns:wsdl="" wsdlLocaption="http://localhost:8888/Service1.svc?wsdl" xmlns=""> <bindings node="wsdl:definitions"> <enableAsyncMapping>true</enableAsyncMapping> </bindings> </bindings>

The parameter “enableAsyncMapping” means generating the asynchronous method communicating to WCF server. Save this file, and execute the following command:

bin\wsimport -extension -keep -Xnocompile -b TestService.xml http://localhost:8888/Service1.svc?wsdl

Then you can find two new directories in Metro folder: “org” and “com”. They are the generated Java code.

  1. Open Eclipse IDE, create a new Java project named “SimpleWCFClient”, and copy the two new directories “org” and “com” to the “src” folder of the project. Refresh the project, and you can find that some new code files are in the project.

  2. Create a test class named “WCFTest” and write the following code to the file:

import; import javax.xml.namespace.QName; import; import org.tempuri.IService1; import org.tempuri.Service1;

public class WCFTest { public static void main(String[] strArgs) { try { Service1 service1 = new Service1(new URL(“http://localhost:8888/Service1.svc?wsdl”), new QName(“”, “Service1”)); IService1 port = service1.getBasicHttpBindingIService1(); ((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, “http://localhost:8888/Service1.svc”); int input = 100; String strOutput = port.getData(input); System.out.println(strOutput); } catch(Exception e) { e.printStackTrace(); } } }

  1. Four .jar files need to be added to the Java project. You can get these files in the “lib” folder of Metro: webservices-api.jar, webservices-extra.jar, webservices-extra-api.jar, webservices-rt.jar. Then go to Property of the project and add these jars to the project.

  2. Compile and run the Java project. If the Eclipse console outputs “You entered: 100”, congratulations, you are successful.

You can download these code from here. When you are familiar with these, you will find it’s very convenient to write a Java application communicating with a WCF client.

Search Blog Posts