Article ID: 321863 - Last Review: April 29, 2007 - Revision: 5.3

HOW TO: Write a Simple Web Service by Using Visual J# .NET

System TipThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
This article was previously published under Q321863

On This Page

Expand all | Collapse all

SUMMARY

This step-by-step article describes how to write a simple Web service and provides an example of how to use the Web service.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Visual Studio .NET
  • Visual J# .NET
  • Microsoft Internet Information Services (IIS) version 5.0 or later
This article assumes that you are familiar with the following topics:
  • Visual Studio .NET Integrated Development Environment (IDE)

Write a Web Service


  1. Start Visual Studio .NET.
  2. Click New Project.
  3. In the project list, click to select Visual J#.
  4. Click to select the ASP.NET Web Service template.
  5. Change the name to MathService.
  6. In Solution Explorer, right-click the Service1.asmx file, and then select View Code.
  7. Add the following methods to the Web Service class:
    /** @attribute WebMethod () */ 
    public float Add (float A, float B) {return (A + B);}
    
    /** @attribute WebMethod () */ 
    public float Subtract (float A, float B) {return (A - B);}
    
    /** @attribute WebMethod () */ 
    public float Multiply (float A, float B) {return (A * B);}
    
    /** @attribute WebMethod () */ 
    public float Divide (float A, float B)
    {
       if (B == 0) return -1;
       
       return Convert.ToSingle (A / B);
    }
            
    					
  8. Save the project.
  9. Use the Build menu to build the Web service.
  10. Press F5 to run the Web service, or use the Web browser to locate http://localhost/Web Service Project Name/Default Web Service Name (for example, http://localhost/Example/Example.asmx). This Web page will allow you to test the Web service methods.

Use the Web Service

  1. Add a new project to the solution. To do this, right-click the solution, click Add, and then click New Project.
  2. In the project list, click to select Visual J#.
  3. Click to select the Console Application template.
  4. Change the name from ConsoleApplication1 to MathApp.
  5. Right-click the new console application, and then click Set as Startup Project.
  6. Right-click the console application again, and then click Add Web Reference.
  7. Type the following URL in the browser Address field to locate the Web service:
    http://localhost/MathService/Service1.asmx
  8. Click Add Reference.
  9. Expand the Web References section of Solution Explorer, and then note the namespace that was used.
  10. In the console application, click Class1.jsl.
  11. Paste the following namespaces at the top of the code page:
    import System.*;
    import System.Console.*;
             
    					
  12. Add the following code to the main method:
                localhost.Service1 myMathService = new localhost.Service1 ();
    
                Console.WriteLine ("2 + 4 = " + myMathService.Add (2,4));
                Console.WriteLine ("4 - 2 = " + myMathService.Subtract (4,2));
                Console.WriteLine ("2 * 4 = " + myMathService.Multiply (2,4));
                Console.WriteLine ("4 / 2 = " + myMathService.Divide (4,2));
    
                Console.ReadLine ();
             
    					
  13. Save the project.
  14. Use the Build menu to build the console application.
  15. Press F5 to run the console application.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:
Programming the Web with XML Web Services
http://msdn.microsoft.com/en-us/library/9t8zkaxa(VS.80).aspx (http://msdn.microsoft.com/en-us/library/9t8zkaxa(VS.80).aspx)
XML Web Services Created Using ASP.NET and XML Web Service Clients
http://msdn.microsoft.com/en-us/library/aa719556.aspx (http://msdn.microsoft.com/en-us/library/aa719556.aspx)
Web Services Essentials, MSDN Online Library
http://msdn2.microsoft.com/en-us/library/9t8zkaxa(VS.80).aspx (http://msdn2.microsoft.com/en-us/library/9t8zkaxa(VS.80).aspx)
Extreme XML: XML Web Service-Enabled Office Documents (MSDN Voices column)
http://msdn.microsoft.com/en-us/library/ms950767.aspx (http://msdn.microsoft.com/en-us/library/ms950767.aspx)
Extreme XML: UDDI: An XML Web Service (MSDN Voices column)
http://msdn.microsoft.com/en-us/library/ms950813.aspx (http://msdn.microsoft.com/en-us/library/ms950813.aspx)
DHTML Dude: Accessing Web Services From DHTML (MSDN Voices column)
http://msdn.microsoft.com/en-us/library/bb263974(VS.85).aspx (http://msdn.microsoft.com/en-us/library/bb263974(VS.85).aspx)
For more information, see the Web Services Description Language Tool (Wsdl.exe). This is one of the Microsoft .NET Framework Tools.


APPLIES TO
  • Microsoft Visual J# .NET 2003 Standard Edition
Keywords: 
kbwebservices kbhowtomaster KB321863