Web Services for .NET and J2EE Interoperability


Introduction

To develop distributed systems, Component based Programming is widely accepted. So far number of technologies has emerged to help building distributed systems within the enterprise. Most common Technologies are:

  • Distributed Component Object Model (DCOM)
  • Common Object Request Broker Architecture (CORBA)
  • Remote Method Invocation (RMI)

But in general, the concept of distributed computing stays within the boundaries of the organization as a whole. This is largely due to the ports and protocols to which these organizations are bound. For security reason, many developers and architects are restricted to using only ports 80 and 443 - the ports for HTTP and HTTPS. Even after various attempts, unfortunately for various reasons, no platform fully convinced enterprise architects to run a distributed architecture across the internet.

Interoperability is the ability for different applications to work together, even though they are running on different operating systems, on different hardware architectures, and using different application infrastructure. Programmable web sites that directly link organizations, application, and services would better meet their business needs. This direct linking of applications can be done easily using Web Services and Interoperability is one of the great promises made by the Web services architecture.

Web services is a standardized way of integrating Web-based applications using the XML, SOAP, WSDL and UDDI open standards over an Internet protocol backbone. And they must address these three key issues to be standardized:

  • Internet Standards
    HTTP and HTTPS must be the initial underlying protocols for Web services to succeed but the technology should be flexible enough to allow any transport protocol to be used.
  • Type definitions
    Data types that were exposed by Web services must use strongly typed XML.
  • Multiple language, platform and vendor support
    Web services must not be tied to a particular platform, language or vendor.

The Five components that make up the technical definition of a Web services are:

  1. Transport (HTTP)
    Currently, generally accepted transport protocol is HTTP (with HTTPS for secure connections). However, this doesn't mean that Web Services are tied to HTTP. They can also run on SMTP, SPX, NetBEUI and even on a custom sockets implementation.
  2. Encoding (XML)
    Regardless of the transport protocol used to carry it, the message must be correctly formatted as a valid XML document.
  3. Standard Structure (SOAP)
    XML defines how the message is encoded but doesn't dictate the structure or format of the document. SOAP is a three part definition that specifies an XML structure for the framework of a message known as SOAP envelope:
    • The data types that are defined
    • A convention for representing RPCs and
    • The data returned from them.
  4. Description (WSDL)
    WSDL completely describes the components being exposed and gives the name, data types (using XML Schema Definition), methods and parameters required to call them.
  5. Discovery (UDDI)
    Universal Description Discovery and Integration provides the exact URL of that particular Web Service.

The strongest reasons why we opted Web services for interoperability are:

  • Application to application communication across the internet
    XML Web Services provide standard defined interfaces called contracts that described the services that they provide.
  • Language independent
    A XML Web service or a connection to an XML Web Services can be written in any language provided it maintains the XML Web Services Standards.
  • Protocol independent
    Since they are communicate by using standard Web protocols and data formats, such as HTTP, XML and SOAP, any server that support these standards can access or host XML Web Services.
  • Platform independent
    Because XML Web Services are accessible through a standard interface, they allow disparate systems to work together.
  • Stateless architecture
    Each response from a Web services is a new object with a new state. (With ASP.net State Management services to maintain state between requests, we can do)
  • Asynchronous
    Since the request object from the client application and response object from the XML Web Service are unique SOAP Envelopes, they are asynchronous. They do not require a shared connection.
  • Based on W3C
    XML Web services are based on World Wide Web Consortium (W3C).

Consuming Java Web Services using a .NET Client.

Let us start our interoperability project by creating a Java Web Services and consuming it by a .NET Client.

Creating a java Web service:

Creating web services in java becomes very simple with lots of tools like AXIS and GLUE in to the picture. Here, I am using Rational Application Developer 6.0 to create the web service.

  1. Start the  Rational Application Developer Studio
  2. Select File>New>Project launching the new Project wizard.
  3. Expand the web folder and select Dynamic Web Project and click next.
  4. In the Dynamic Web Project window enter CincomWS and click Finish.
  5. Within the project explorer, expand CincomWS and Java Resources folder.
  6. Right click the JavaSource folder and select New > Class.
  7. Enter the package name as com.cincom and class name as Welcome.
  8. Now create a method hiFromCincom and make sure the Welcome.java contains the following lines.

    package com.cincom;
    public class Welcome
    {
              public String hiFromCincom(String strNameFromClient)
              {
                        return "HI " + strNameFromClient + ", Welcome to Cincom Java Web Services World.";
              }
    }

  9. Select File > New > Other.
  10. Select show all wizards and select Web Service. Click Next.
    NOTE: A window might appear telling you that you need to activate Web Service Development features. Select Always enable capabilities and don't ask again.
  11. In the Web Services window, select Generate a proxy. You can choose the Test the Web Service option also for creating a test client.
  12. Click Next.
  13. In the next window, enter com.cincom.Welcome as the bean and click Next.
  14. Select the Service web project cincomWS and EAR Project cincomWSEAR.
  15. In the Java Bean Identity window, check the hiFromCincom method and click Finish.

Now our first Java web service is created. We can see the wsdl with the link

http://localhost:9080/CincomWS/com/cincom/wsdl/Welcome.wsdl

Consuming the web service using .NET client:

  1. Open the Microsoft Visual Studio .NET
  2. Select File > New > Project then Visual C# projects and Asp.net Web application and enter http://localhost/WSClient to the location field.
  3. Add a reference of the above web service.
    This can be done in two ways:

    I-Using Visual Studio .NET
    • Right click on the References and click Add web reference...
    • Enter http://localhost:9080/CincomWS/com/cincom/wsdl/Welcome.wsdl to the URL field of the Add web reference and click go.
    • Enter cincomWS to the Web reference name field and click add reference
    II-Using WSDL.exe
    • Open a DOS Command prompt.
    • Run wsdl http://localhost:9080/CincomWS/com/cincom/wsdl/Welcome.wsdl it will create WelcomeService.cs file
    • Then run
       csc /t:library /r:System.dll,System.Web.Services.dll,System.Xml.dll   WelcomeService.cs
    • The above will created WelcomeService.dll
    • Now copy this dll to bin folder of WSClient folder or add a reference (not web reference)  of it to the WSClient project.
    NOTE: The proxy file created by WSDL.exe is static while the one that are created automatically by the Web reference is dynamic. If we want adding authentication, using WSDL.exe is the best approach as you can guarantee that the file will remain under your control.
  4. In the design view of the WebForm1.aspx, Add a Text box, one Label and one Button.
  5. Double click on the Button and make sure the Click function of the button is as follows.
     

    private void Button1_Click(object sender, System.EventArgs e)

    {

              cincomWS.Welcome cs = new cincomWS.Welcome();

              //if You are using the WelcomeService.dll comment the above    

          //line add a reference of System.Web.Services

          //and uncomment the line bellow

              //WelcomeService cs = new WelcomeService();

              String strName;

              if (TextBox1.Text.Equals(""))

              {

                        strName = "Client";

              }

              else

              {

                        strName = TextBox1.Text;

              }

              Label1.Text = cs.hiFromCincom(strName);
    }


  6. Now run the project.
  7. Put a name (YOURNAME) in the Text Box and then click the Button.
  8. The text of the Label will change to

    HI YOURNAME, Welcome to Cincom Java Web Services World.

Consuming .NET Web Services using a java Client.

Now, in this section we are going to create a .NET Web Services and consuming it by a java Client.

Creating a .NET Web Services:

  1. Start Visual Studio .NET and Select New > Project > Visual C# Project > ASP.NET Web Service
  2. Write http://localhost/cincomWS on the location field.
  3. Rename the Service1.asmx to Welcome.asmx
  4. Create a web method called hiFromCincom as follows

    [WebMethod]

    public string hiFromCincom(String strNameFromClient)

    {

              return "HI " + strNameFromClient + ", Welcome to Cincom .NET Web Services World.";

    }

  5. Now run the project
  6. Check the url http://localhost/cincomWS/Welcome.asmx
  7. And check http://localhost/cincomWS/Welcome.asmx?WSDL for WSDL

Consuming the web service using java client:

For creating the java proxies for that .NET Web service, we are going to use GLUE, by The Mind Electric, which is a framework for developing and publishing Web services. Get an evaluation version from http://www.webmethods.com/meta/default/folder/0000008629 and install it.

  1. Run the DOS command prompt
  2. Make a folder for your own choice say JavaClient
  3. Change the current working directory JavaClient
  4. Run the command wsdl2java http://localhost/cincomWS/Welcome.asmx?WSDL
    Two files are generated.
     IWelcomeSoap.java and WelcomeHelper.java
  5. Now create a java file Client.java in the same folder
  6. Past the following code to the file

    public class Client

    {

      public static void main(String[] args) throws Exception

      {

        String url = "http://localhost/cincomWS/Welcome.asmx?WSDL";  

              IWelcomeSoap welcome = WelcomeHelper.bind();

              String strName;

              if (args.length == 0)

              {

                        strName = "Client";

              }

              else

              {

                        strName = args[0];

              }

        System.out.println("\n"+ welcome.hiFromCincom(strName));    

      }

    }

  7. Now compile the files with javac *.java
  8. Run the client java Client YOURNAME

    You will get an output as

    HI YOURNAME, Welcome to Cincom .NET Web Services World.

Up Next
    Ebook Download
    View all
    Learn
    View all