Creating Web Services

Team-Fly    

Developing XML Web Services and Server Components with Visual C#™ .NET and the .NET Framework, Exam Cram™ 2 (Exam 70-320)
By Amit Kalani, Priti Kalani

Table of Contents
Chapter 5.  Basic Web Services


To better understand Web services, you should be familiar with both sides of the conversation. In this section, you learn how to create a Web service.

Creating a Web Service Project

In this section, you learn how to create your own Web service. Take the following steps to create a simple string-manipulation Web service:

  1. Create a new Visual C# project based on the ASP.NET Web Service template. Name the project Example5_2.

  2. Right-click the Service1.asmx file in Solution Explorer and rename it Strings.asmx.

  3. Click the hyperlink on the Strings.asmx design surface to switch to code view. Modify the name of the class and the constructor from Service1 to Strings.

  4. Add the following attribute definition before the Strings class declaration:

     [WebService(Namespace="http://techcontent.com/EC70320/C05/Example5_2")] public class Strings : System.Web.Services.WebService 
  5. Enter the following methods in the class definition:

     [WebMethod()] public String ToUpper(String inputString) {    return inputString.ToUpper(); } [WebMethod()] public String ToLower(String inputString) {     return inputString.ToLower(); } 
  6. Select Build, Build Solution to create the Web service on the server.

You now have a functioning Web service on your Web server. Although properly hooking up a Web service is complex, Visual Studio .NET protects you from having to set up any of it. Instead, you only have to do the following three things:

  1. Build the project from the ASP.NET Web Service template.

  2. Mark the classes that should be available via the Web service with the WebService attribute.

  3. Mark the methods that should be available via the Web service with the WebMethod attribute. The methods marked with the WebMethod attributes are also known as Web methods.

graphics/note_icon.gif

The WebService attribute requires you to supply a value for the Namespace property. This value can be purely arbitrary and need not resolve to any actual Web site this string is just a unique identifier for your Web service. If you do not change the default value, Visual Studio .NET gives you a warning.


Testing the Web Service Project

ASP.NET enables you to test a Web service project without building any client applications for the Web service. This can save a lot of time when you're debugging a Web service. Follow these steps:

  1. Set Example5_2 as the startup project for the solution and select Debug, Start to start the project. This launches a browser window, showing the test page.

  2. Click the ToUpper link on the test page. A page for testing the ToUpper() method (shown in Figure 5.2) appears.

    Figure 5.2. The Web method test page enables you to test the Web method using the HTTP GET protocol.

    graphics/05fig02.jpg

  3. Enter a string with mixed upper- and lowercase characters at the inputString prompt, and click the Invoke button. A second browser window opens with the XML message that the Web service sends back when you call the ToUpper() method on the test string.

  4. Experiment with the ToLower() method in the same way you experimented with the ToUpper() method. When you click the Invoke button, the test page constructs the appropriate XML message and passes it to the Web service, which returns the results.


    Team-Fly    
    Top


    MCAD Developing XML Web Services and Server Components with Visual C#. NET and the. NET Framework Exam Cram 2 (Exam Cram 70-320)
    Managing Globally with Information Technology
    ISBN: 789728974
    EAN: 2147483647
    Year: 2002
    Pages: 179

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net