Creating Web Services

To better understand Web services, you should be familiar with both sides of the conversation.

Creating a Web Service Project

In this section you learn how to create a Web service of your own. 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 Example9_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. Then add the following attribute definition before the Strings class declaration:

     [WebService(Namespace="http://techcontent.com/315C09/Example9_2")] public class Strings : System.Web.Services.WebService 
  4. Modify the name of the constructor from Service1 to Strings .

  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 a lot is involved in properly hooking up a Web service, Visual Studio .NET protects you from having to set up any of it. Instead, you only have to do 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 is just a unique identifier for your Web service.


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. Here's how:

  1. Set Example9_2 as the startup project for the solution and select Debug, Start to start the project. A browser window is launched, showing the test page.

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

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

    graphics/09fig03.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.



MCAD Developing and Implementing Web Applications with Visual C#. NET and Visual Studio. NET (Exam [... ]am 2)
MCAD Developing and Implementing Web Applications with Visual C#. NET and Visual Studio. NET (Exam [... ]am 2)
ISBN: 789729016
EAN: N/A
Year: 2005
Pages: 191

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