Microsoft KB Archive/321863

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

Article ID: 321863

Article Last Modified on 4/29/2007

-

APPLIES TO


 * Microsoft Visual J# .NET 2003 Standard Edition
 * Microsoft Visual J# .NET 2003 Standard Edition

-



This article was previously published under Q321863



For a Microsoft Visual Basic .NET version of this article, see 301273.

For a Microsoft Visual C# .NET version of this article, see 308359.

IN THIS TASK
SUMMARY
 * Requirements
 * Write a Web Service
 * Use the Web Service

REFERENCES



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.

back to the top

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)

back to the top

Write a Web Service
 Start Visual Studio .NET. Click New Project. In the project list, click to select Visual J#. Click to select the ASP.NET Web Service template. Change the name to MathService. In Solution Explorer, right-click the Service1.asmx file, and then select View Code.  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); }                   </li> Save the project.</li> Use the Build menu to build the Web service.</li> Press F5 to run the Web service, or use the Web browser to locate http://localhost/ / (for example, http://localhost/Example/Example.asmx). This Web page will allow you to test the Web service methods.</li></ol>

back to the top

Use the Web Service
<ol> Add a new project to the solution. To do this, right-click the solution, click Add, and then click New Project.</li> In the project list, click to select Visual J#.</li> Click to select the Console Application template.</li> Change the name from ConsoleApplication1 to MathApp.</li> Right-click the new console application, and then click Set as Startup Project.</li> Right-click the console application again, and then click Add Web Reference.</li> Type the following URL in the browser Address field to locate the Web service:

http://localhost/MathService/Service1.asmx

</li> Click Add Reference.</li> Expand the Web References section of Solution Explorer, and then note the namespace that was used.</li> In the console application, click Class1.jsl.</li>  Paste the following namespaces at the top of the code page: import System.*; import System.Console.*; </li>  Add the following code to the main method: localhost.Service1 myMathService = new localhost.Service1 ;

Console.WriteLine (&quot;2 + 4 = &quot; + myMathService.Add (2,4)); Console.WriteLine (&quot;4 - 2 = &quot; + myMathService.Subtract (4,2)); Console.WriteLine (&quot;2 * 4 = &quot; + myMathService.Multiply (2,4)); Console.WriteLine (&quot;4 / 2 = &quot; + myMathService.Divide (4,2));

Console.ReadLine ; </li> <li>Save the project.</li> <li>Use the Build menu to build the console application.</li> <li>Press F5 to run the console application.</li></ol>

back to the top

<div class="references_section">