May 07 2009

Using different data types between Java and WCF

Published by Kevin Gao under WCF & Java Interop

This article is a part of WCF & Java Interop

Many C# or Java data types can be described by using XML Schema so that we can use XML Schema to communicate between Java and WCF.

 

  1. Primitive Data Types
  2. Primitive data types such as byte, short, int, long, float, double, boolean, char are almost the same in C# and Java. You can use these data types in WCF Contract normally. But there are some points you must pay attention to:
            using short in Java when the type is byte in C#;
            using int in Java when the type is char in C#.

    Here, I listed the details of primitive data types:

    Java Client Data Type

    C# WCF Data Type

    short

    byte

    short

    short

    int

    int

    long

    long

    float

    float

    double

    double

    Boolean

    Boolean

    int

    char

  3. String
  4. C# has a System.String class which is analogous to the java.lang.String class. In C#, the string class can be written as string or String.

     

  5. Enumeration
  6. WCF sends and receives an enumeration value with its name in the String format. For example, for an enumeration variable (C#) as follows:
    public enum EnumColor
    {
        enumBlack = 0,
        enumWhite = 1,
        enumRed = 2
    }

    The generated code is as follows:
    @XmlType(name = "EnumColor")
    @XmlEnum


    public enum EnumColor {
        @XmlEnumValue("enumBlack")
        ENUM_BLACK("enumBlack"),
        @XmlEnumValue("enumWhite")
        ENUM_WHITE("enumWhite"),
        @XmlEnumValue("enumRed")
        ENUM_RED("enumRed");
        private final String value;
        EnumColor(String v) {
            value = v;
        }
        public String value() {
            return value;
        }
        public static EnumColor fromValue(String v) {
            for (EnumColor c: EnumColor.values()) {
                if (c.value.equals(v)) {
                    return c;
                }
            }
            throw new IllegalArgumentException(v);
        }
    }

  7. Complex Types: Structure or Class
  8. [DataContract]
    public class CompositeType {
        bool boolValue = true;
        string stringValue = "Hello ";
        [DataMember]
        public bool BoolValue {
            get { return boolValue; }
            set { boolValue = value; }
            }
        [DataMember]
        public string StringValue {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }

    “[DataContract]” and “[DataMember]” make a class or a structure serializable. If a member variable is declared as a [DataMember], it will be serialized. Then you can write the following code in Java class:
    ObjectFactory of = new ObjectFactory();
    CompositeType composite = of.createCompositeType();
    composite.setBoolValue(true); composite.setStringValue(of.createCompositeTypeStringValue("world"));
    CompositeType returnComposite = port.getDataUsingDataContract(composite);

     

  9. Array, List
  10. The Array or List data types can be used as parameters in WCF Operation Contract and the generated Java code of these two data types is the same. Metro generates a new data type by adding a prefix “ArrayOf”. For example: List and String[] are both translated to ArrayOfstring. This class can be used in Java as follows:
    ArrayOfstring arrayValue = new ArrayOfstring();
    arrayValue.getString().add("This");

     

  11. DateTime
  12. There is a data type named “DateTime” in C#. It describes date, time and time zone information. For the DateTime type, Metro generates Java code with the corresponding type “XMLGregorianCalendar”. You can use the XMLGregorianCalendar type directly. However, Java generally uses “GregorianCalendar” to declare date time variable. Therefore, it is necessary to make a conversion between GregorianCalendar and XMLGregorianCalendar.
    /*Convert GregorianCalendar to XMLGregorianCalendar*/
    public XMLGregorianCalendar convertCalendarToXMLCalendar(GregorianCalendar objGregorianCalendar)
    {
        XMLGregorianCalendar objXMLGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(objGregorianCalendar);
        return objXMLGregorianCalendar;
    }

    /*Convert XMLGregorianCalendar to GregorianCalendar*/
    public GregorianCalendar convertXMLCalendarToCalendar(XMLGregorianCalendar objXMLGregorianCalendar)
    {
        objXMLGregorianCalendar.objXMLGregorianCalendar.toGregorianCalendar();
    }

Download source code

 

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

3 responses so far

May 07 2009

Data types between Java and WCF

Published by Kevin Gao under WCF & Java Interop

This article is a part of WCF & Java Interop

SCM Anywhere is a comprehensive tool and uses various C# data types, including lists. You may doubt whether Java can recognize these C# data types. Actually, WCF uses Web Services Description Language (WSDL) to describe services and uses XML Schema to describe the data types used in Messages. For more information about XML Schema for data types, you can refer to: XML Schema Part 2: Datatypes Second Edition.

 

WSDL is language-independent and supported by Microsoft, IBM, and other vendors. WCF provides a rich infrastructure for exporting, publishing, retrieving, and importing service metadata. WCF services use metadata to describe how to interact with the service’s endpoints so that tools, such as wsimport, can automatically generate client code for accessing the service.

 

C# basic data types can be described in WSDL as a specific format. Metro generates Java client code from the WSDL document. For example, the syntax of the “int” data type is as follows:

<xs:element minOccurs="0" name="value" type="xs:int" />

In all, many data types can be used between Java and WCF. I will talk about some common data types between Java and WCF in the next article.

 

Links:
Previous article >>>>: Java client and WCF server
Next article >>>>: Using different data types between Java and WCF
WCF & Java Interop series home page: WCF & Java Interop

No responses yet

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

3 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

One response 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

2 responses so far

Mar 28 2009

Microsoft released Silverlight 3 Beta !

Published by Kevin Gao under Programming

Microsoft has just released Silverlight 3 Beta. There are big improvements in this release. It is good news for us, since we are developing a Silverlight 2 application. As a professional software development company developing tools for software development teams, we are not very interested in the rich media part of the new features. The parts that we are interested in are: 60+ controls with source code and out-of-browser capability. With the out-of-browser support, the applications built with Silverlight 3 can run both within and outside a browser. The auto update feature is also nice since the new version of our software can be downloaded and installed automatically on the client side.

You can read more at this page:
http://silverlight.net/getstarted/silverlight3/default.aspx

No responses yet

Next »

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