Blue Theme Orange Theme Green Theme Red Theme
 
Safari Books Online
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
ANTS Performance Profiler 6.0
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Web Services » Web Services between .NET, Java and MS SOAP Toolkit: Part I

Web Services between .NET, Java and MS SOAP Toolkit: Part I


WinGhost 2.0 application allows you to show and hide applications running on your machine. I find this program useful at my work as I do lot of programming at any given time I can have a bunch windows open. I use it normally to hide just my windows I don't access that often.

Total page views :  2953
Total downloads :  21
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
WebServiceArticleCT.zip
 
ASP.Net 4 Hosting is here
Become a Sponsor

This article will try to explain the how you can build web services and/or clients with any of the three languages: .NET, MS SOAP Toolkit and Java. But the real point of the article is to show you how you can build clients for web services from any of above-mentioned languages. 

Not long ago .NET was released and many of us jumped on to write ASP.NET web sites, C# programs or Web Services. I was really amazed by the relatively easy way someone can write a web service with .NET. I previously had written some web services with MS SOAP Toolkit and Apache SOAP for Java. And then someone asked me to write clients using different languages for those web services. It proved to be not trivial.

Although SOAP is now a standard, different implementations of web services are using it in ways that sometime makes the interoperability with other SOAPs hard if not impossible. 

My sample application is a very simple web service with one method: addNumbers. As you already deducted it will add two numbers and return the result. The name of the application is Hello2 and the source files are attached to this message. 

STK Service and Clients

First lets write the Web Service using the MS SOAP Toolkit with an ASP listener and an ISAPI listener as well.The addNumbers method in the Visual Basic class is:

Public Function addNumbers(ByVal NumberOne As Double, ByVal NumberTwo As Double) As Double
addNumbers = NumberOne + NumberTwo
End Function

Using the WSDLGen.exe wizard you can generate the ISAPI listener, the ASP listener or both (not at the same time, of course). I choosed to generate both the ASP and the ISAPI listener, so I named my WSDL files Hello2ASP.WSDL respectively Hello2Isapi.WSDL. Now lets write some clients for this Hello2 web service. 

STK Client

The first client is a Visual Basic client using high level API in SOAP Toolkit. Create a VB project add a form and then a button. The code bellow is executed when the button is hit.

Private Sub cmdDoTest_Click()
Const WS_URL = http://localhost/Hello2/Hello2Isapi.WSDL
Dim objHello2ISapi As SoapClient
Dim nResult As Double, NumberOne As Double, NumberTwo As Double
On Error GoTo catch_err
objHello2ISapi =
New SoapClient
Call objHello2ISapi.mssoapinit(WS_URL)
NumberOne = 10
NumberTwo = 25
nResult = objHello2ISapi.addNumbers(NumberOne, NumberTwo)
MsgBox(nResult)
cleanup:
objHello2ISapi =
Nothing
Exit Sub
catch_err:
MsgBox(Err.Description)
Resume cleanup
End Sub

As you can see the client is pretty simple and there are no problems. All the details like building the SOAP request message and parsing the result SOAP message is hidden from the programmer.

WS_URL is the URL of the service. The high level API in SOAP Toolkit needs a WSDL file so this URL points to one of the WSDL files. At this point it doesnt matter which one you provide. Though, the performance is better with the ISAPI listener. 

Java Client

The second client well write for our Hello2 server is a Java client. I used for this the Apache SOAP 2.1. You can download it for free from http://xml.apache.org/soap/index.htmlThe Java class file for the ASP listener is listed bellow. 

import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
import org.apache.soap.transport.http.SOAPHTTPConnection;
public class testClient {
public static void main(String[] args) throws Exception {
URL url =
new URL (http://localhost/Hello2/Hello2.asp);
SOAPMappingRegistry smr =
new SOAPMappingRegistry ();
StringDeserializer sd =
new StringDeserializer ();
smr.mapTypes (Constants.NS_URI_SOAP_ENC,
new QName ("", "Result"), null, null, sd);
// create the transport and set parameters
SOAPHTTPConnection st = new SOAPHTTPConnection();
// build the call.
Call call = new Call ();
call.setSOAPTransport(st);
call.setSOAPMappingRegistry (smr);
call.setTargetObjectURI (http://tempuri.org/message/);
call.setMethodName("addNumbers");
call.setEncodingStyleURI (http://schemas.xmlsoap.org/soap/encoding/);
Vector
params = new Vector();
params.addElement(new Parameter("NumberOne", Double.class, "10", null));
params.addElement(new Parameter("NumberTwo", Double.class, "25", null));
call.setParams(
params);
Response resp =
null;
try {
resp = call.invoke (url, http://tempuri.org/action/Hello2.addNumbers);
}
catch (SOAPException e) {
System.err.println("Caught SOAPException (" + e.getFaultCode () + "): " +e.getMessage ());
return;
}
// check response
if (resp != null && !resp.generatedFault()) {
Parameter ret = resp.getReturnValue();
Object
value = ret.getValue();
System.
out.println ("Answer--> " + value);
}
else {
Fault fault = resp.getFault ();
System.err.println ("Generated fault: ");
System.
out.println (" Fault Code = " + fault.getFaultCode());
System.
out.println (" Fault String = " + fault.getFaultString());
}
}
}

As you can see the url variable points to the ASP listener. To point the Java client to the ISAPI listener make the following change:

URL url = new URL ("http://localhost/Hello2/Hello2Isapi.wsdl");

.NET Client

Now it is time to write a .NET client for our Hello2 Web service. Using the WSDL.exe tool from .NET Framework Beta 2 you must generate a proxy class for our service. Execute following command. 

wsdl http://localhost/Hello2/Hello2Isapi.wsdl 

This will generate Hello2Isapi.cs file. This is the .NET proxy class using C# (this is the default language used). Check out the params for wsdl.exe to generate the proxy using VB.NET or another language.

Now compile the proxy using 

csc.exe /t:library Hello2Isapi.cs 

Its time to write the .NET client, which uses the proxy class to access the Hello2 web service. Here is the code for the C# client.

using System;
public class Hello2ISapiClient
{
public static void Main()
{
Hello2Isapi srv =
new Hello2Isapi();
double res = 0, num1 = 10, num2 = 25;
res = srv.addNumbers(num1, num2);
Console.WriteLine("{0}+{1}={2}", num1, num2, res);
}
}

Compile the client using Hello2IsapiClient.cs /reference:Hello2Isapi.dll and run it with Hello2IsapiClient. 

At this point we have a MS SOAP Toolkit web service and three clients written with: SOAP Toolkit, Java respectively .NET 

Apache SOAP for Java Service and Clients

Lets move on now and write the same service using Apache SOAP for Java. Here is the service: 

package samples.MyService; 
import java.util.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
public class MyService {
public double addNumbers(double num1, double num2) {
return num1+num2;
}
}

I used the name MyService for my service and I added it to the samples package. This way you dont need to add a context into the Tomcat server. Just deploy the service into SOAP using the following deployment descriptor file: 

<isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment" id="urn:myservice-service" checkMustUnderstands="false"> <isd:provider type="java" scope="Application" methods="addNumbers">
<isd:java class="samples.MyService.MyService" static="false"/>
</isd:provider>
</isd:service>

I wont explain here how to set up Apache SOAP into Tomcat since there is enough guidance in the Apache SOAP documentation.

Apache SOAP Client

Its time to write the clients for this service. The first one is written using Java. Here is the code:

package samples.MyService;
import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
public class client {
public static void main(String[] args) throws Exception {
if (args.length != 3
&& (args.length != 4 || !args[0].startsWith("-")))
{
System.err.println("Usage:");
System.err.println(" java " + client.
class.getName() +" [-encodingStyleURI] SOAP-router-URL nameToLookup");
System.exit (1);
}
// Process the arguments.
int offset = 4 - args.length;
String encodingStyleURI = args.length == 4? args[0].substring(1): Constants.NS_URI_SOAP_ENC;
URL url =
new URL(args[1 - offset]);
Double num1 =
new Double(args[2 - offset]),num2 = new Double(args[3 - offset]);
SOAPMappingRegistry smr =
new SOAPMappingRegistry();
BeanSerializer beanSer =
new BeanSerializer();
System.
out.println(encodingStyleURI);
System.
out.println(url);
System.
out.println(num1);
System.
out.println(num2);
// Build the call.
Call call = new Call();
call.setSOAPMappingRegistry(smr);
call.setTargetObjectURI("urn:MyService");
call.setMethodName("addNumbers");
call.setEncodingStyleURI(encodingStyleURI);
Vector
params = new Vector();
params.addElement(new Parameter("num1", Double.class, num1, null));
params.addElement(new Parameter("num2", Double.class, num2, null));
call.setParams(
params);
// Invoke the call.
Response resp;
long nErrors = 0;
Calendar cal = Calendar.getInstance();
Date startTime = cal.getTime(), endTime;
try {
resp = call.invoke(url, "");
}
catch (SOAPException e) {
System.
out.println("i=" + i);
System.err.println("Caught SOAPException (" +e.getFaultCode() + "): " +e.getMessage());
return;
}
// Check the response.
if (!resp.generatedFault()) {
Parameter ret = resp.getReturnValue();
Object
value = ret.getValue();
//System.out.println(value != null ? "\n" + value : "I don't know.");
}
else {
Fault fault = resp.getFault();
System.err.println("Generated fault: ");
System.
out.println (" Fault Code = " + fault.getFaultCode()); System.out.println (" Fault String = " + fault.getFaultString());
}
cal = Calendar.getInstance();
endTime = cal.getTime();
System.
out.println("Start time="+startTime);
System.
out.println("End time="+endTime);
System.
out.println ("Errors=" + nErrors);
}
}

As you can see the code is pretty straightforward. No problems expected since the same SOAP library is used.

The code for a STK client is bellow:

STK Client

There are errors in both high level and low level clients because xsi:type is required in Apache SOAP for Java.

.NET Client

Due to the same issue a .NET client wont work either.

.NET Service and Clients

.NET Framework Beta 2 is the newest technology and changes are expected to happen from Beta 2 to the final release. Major changes already have been made when Beta 2 was released. This is no surprise since Microsoft had warned developers that might happen.

Writing a web service with .NET is very simple and can be done in several ways. I choose to write my web service in C# using a ASMX file. Here is the content of the file.

<%@ WebService Language="C#" Class="MyService" %>
using System;
using System.Web.Services;
[WebService(Namespace=http://www.catalin.com/webservices/)]
public class MyService: WebService
{
[ WebMethod(Description="return the sum of two numbers")]
[System.Web.Services.Protocols.SoapRpcMethodAttribute("http://www.catalin.com/webservices/addNumbers", RequestNamespace="http://www.catalin.com/webservices/", ResponseNamespace=http://www.catalin.com/webservices/)]
public double addNumbers(double numberOne, double numberTwo)
{
return numberOne + numberTwo;
}
}

The advantage of using a ASMX file is that no compilation is needed so a hot deployment can be done.

Place the file into a virtual directory under IIS. You can test the service with your IE with http://localhost/testdotnetws/myservice.asmx 

.NET Client

Writing a client for this service is similar with the .NET client we write earlier. When generating the proxy file specify the WSDL file with http://localhost/testdotnetws/myservice.asmx?WSDL. This is the way .NET framework creates the WSDL file on-demand. 

STK Client

Using the high level API would be a faster solution but there are some issues there and I couldnt solve them out so I used the low level API. The client code is not difficult. The only trick is to enable the .NET service for RPC type of calls. Thanks to Christian Weyer who helped me with this issue.

Look at the web service code and notice the System.Web.Services.Protocols.SoapRpcMethodAttribute attribute for our method. Without this attribute the default type of conversation in .NET is message. 

Java Client

In the java client youll have to modify the url as follows:

URL url = new URL ("http://localhost/aspnet_test/myservice/myservice.asmx");

Compile and run.

I hope this short trip to the web service world was helpful for those of you who are involved in web services development.

 This article is tested and/or updated to RTM by Dipal Choksi.


Login to add your contents and source code to this article
 About the author
 
Catalini Tomescu
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
SQL and .NET performance profiling in one place
Investigate SQL and .NET code side-by-side with ANTS Performance Profiler 6, so you can see which is causing the problem without switching tools.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from DevExpress - Absolutely Free-of-Charge without any royalties or distribution costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin edit, tab control and so much more!

DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
 Comments

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2010.8.14
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.