Archive for April, 2009

Apr 23 2009

Java client and WCF server

Published by Kevin Gao under WCF & Java Interop

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 Java.net. 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: https://metro.dev.java.net.

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”.

 

5. 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="http://www.w3.org/2001/XMLSchema"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    wsdlLocaption="http://localhost:8888/Service1.svc?wsdl"
    xmlns="http://java.sun.com/xml/ns/jaxws">
    <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.

6. 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.

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

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
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("http://tempuri.org/", "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();
        }
    }
}

8. 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.

9. 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.

 

Links:
Previous article >>>>: WCF client and WCF service
Next article >>>>: Data types between Java and WCF
WCF & Java Interop series home page: WCF & Java Interop

7 responses so far

Apr 22 2009

WCF client and WCF service

Published by Kevin Gao under WCF & Java Interop

This article is a part of WCF & Java Interop

Download source code

 

Introduction

To begin with, we are going to create a very simple WCF service and WCF client in C#/VB.NET first. After this article, we will have a working WCF application. Then in the next article, we will create a Java client to connect to the WCF service created in this article.

 

Creating a WCF service

Using Visual Studio 2008, we can easily create Windows Communication Foundation (WCF) service and client. This topic describes how to create a basic WCF service and a client that can call the service.

To create a basic WCF service, please follow the steps below:

1. Open Visual Studio 2008.

2. Create a new WCF Service Application project. In Visual Studio 2008, click menu File -> New -> Project. In the New Project dialog, expand Visual Basic or Visual C# and choose the WCF Service Application template to create a project named “WcfService1″. Click OK.

 

Now, we have created a basic WCF service application. In Solution Explorer, we can see four files in the “WcfService1″ project.

 

File

Description

IService1.cs

IService1 interface, a WCF service contract

Service1.svc

A service file, accessed by the client as a part of URL

Service1.svc.cs

A class, implements the IService1interface

Web.config

Service configuration file

 

In the “Service1.svc.cs” file, we can find a class named “Service” which includes two functions, “GetData” and “GetDataUsingDataContract”.

You can create a new service or add more functions to the existing service. I will talk about these in the future. Now we can run the server in the Debug mode. The ASP.NET Deployment Server will start and the service will be deployed to the server:

 

Creating a WCF client

To create a WCF client that calls the service, please follow the steps below:

1. Open Visual Studio 2008.

2. Create a new Console Application project. In Visual Studio 2008, click menu File -> New -> Project. In the New Project dialog, expand Visual Basic or Visual C# and choose the Console Application template to create a project named “WcfClient1″. Click OK.

 

3. Add a service reference to the project. Go to Solution Explorer, right-click the “References” node in the “WcfClient1″ project, and click Add Service Reference.

 

4. In the Add Service Reference dialog, input the server URL above in Address. Click Go and then click OK.

 

5. We can find a node named “ServiceReference1″ under Service References.

 

6. Add a using statement (Imports in Visual Basic) for the System.ServiceModel namespace in the generated “Program.cs” file.

using System.ServiceModel;

7. Add the following code into the Main function in the “Program.cs” file:

Service1Client client = new Service1Client();
String strResponse = client.GetData(100);
Console.WriteLine(strResponse);
Console.ReadLine();
client.Close();

8. Then we can run the client application in the Debug mode. If we see the following result, our WCF application is working properly.

 

Links:
Previous article >>>>: WCF and Java Interop Series introduction
Next article >>>>: Java client and WCF server
WCF & Java Interop series home page: WCF & Java Interop

3 responses so far

Apr 20 2009

WCF and Java Interop Series introduction

Published by Kevin Gao under WCF & Java Interop

This article is a part of WCF & Java Interop

WCF and Java Interop Series introduction

This series of articles is about how to use Java (WSIT, METRO) to communicate with applications developed with Windows Communication Foundation (WCF).

Web Service has been accepted globally by developers because of its ease of integration, availability of development tools, and adoption by big players, such as SUN, Microsoft, and Oracle.

In the past, I used to use Sockets to program the network layer. With sockets, we can have a full control over the network layer, and the performance is good. However, it is difficult for third-parties to integrate with our applications. When altering the network interface, it is necessary to change the serialization and deserialization code.

 

WCF Introduction

“Windows Communication Foundation (WCF) is Microsoft’s unified programming model for building service-oriented applications. It provides integration and interoperability with existing .NET Framework distributed systems technologies such as Message Queuing (MSMQ), COM+, ASP.NET Web services, Web Services Enhancements (WSE), and many other functions.”

In a nutshell, WCF does all the network plumbing work for you. You do not need to worry about whether or not you are going to use web service, MSMQ, raw TCP/IP socket or other technologies. For example, you can develop your entire application based on TCP/IP socket with WCF at the beginning, and when you need to switch to web service, you only need to change the endpoint on the server side and regenerate the proxy on the client side. All other parts of your application can remain unchanged.

 

Metro Introduction

Metro is an open source Java-based web service development stack. “The Metro stack consists of JAX-WS, JAXB, and WSIT, which enable you to create and deploy secure, reliable, transactional, and easily integratable Web services and clients.”

 

A Real World Case Study of WCF and Java (Metro) Interoperability

We started Dynamsoft SCM Anywhere project, which is a SCM solution with integrated Version Control, Issue Tracking and Build Automation about a year ago. As the product architect, I am responsible for choosing a proper network technology for our team. After developing several small trial projects, WCF and Metro were chosen. Recently, we have released SCM Anywhere, and the result proves that using WCF and Java (Metro) was a wise choice.

The architecture of SCM Anywhere is as follows:

The architecture of SCM Anywhere
(The architecture of SCM Anywhere)

 

The SCM Anywhere server and Windows client are developed with WCF and programmed in C# with Visual Studio 2008. The cross-platform client is developed with Metro/WSIT and programming in Java with Eclipse.

We use wsimport tool in Metro to generate Java client-side artifacts by importing a WSDL file (server side). Then, we implement the Java client to invoke WCF services. Developing the Windows client with WCF is straightforward, and the tool is integrated into Visual Studio 2008.

 

Links:
Next article >>>>: WCF client and WCF service
WCF & Java Interop series home page: WCF & Java Interop

4 responses so far

Version Control Software/System | Source Control Software/System | Software Configuration Management | SCM Hosting Solution | Bug Tracking System
SourceSafe (VSS) Replacement/Alternative | SourceSafe (VSS) Hosting | SourceSafe (VSS) Remote/Web/Internet Access | Scanner COM
Customer Service Software | Live Chat | Live Help | Forum Software | Knowledge Base Software | Newsletter Email Marketing Software