Creating a Simple ASP.NET Web Service


In the following Try It Out, you create a simple Web service with Visual Studio.

Try It Out – Creating a Web Service Project

image from book
  1. Create a new Web Service with File New Web Site..., choose the ASP.NET Web Service template as shown in Figure 20-8, name the project WebServiceSample, and click the OK button.

    image from book
    Figure 20-8

image from book

Generated Files

The files generated by the wizard are listed here:

  • Service.asmx holds your Web service class. All ASP.NET Web services are identified with the .asmx extension. The file that has the source code is Service.cs, as the code-beside feature is used with Visual Studio 2005. This file can be found in the App_Code directory.

  • The wizard generates a class Service that derives from System.Web.Services.WebService. In the Service.cs file, you can also see some sample code showing how a method for a Web service should be coded — it should be public and marked with the WebMethod attribute:

     using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = http://tempuri.org)] [WebServiceBinding(ConformsTo=WsiProfile.BasicProfile1_1)] public class Service : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } } 

Adding a Web Method

The next thing you should do is add a custom method to your Web service. In the following Try It Out, you add a simple method ReverseString() that receives a string and returns the reversed string to the client.

Try It Out – Adding a Method

image from book
  1. Remove the method HelloWorld() with the complete implementation. Add the following code to the file Service.cs:

     [WebMethod] public string ReverseString(string message) { char[] arr = message.ToCharArray(); Array.Reverse(arr); message = new string(arr); return message; } 

    To use the Array class, you must import the System namespace.

    To uniquely identify the XML elements in the generated description of the Web service, a namespace should be added. Add the WebService attribute with the namespace http://www.wrox.com/ webservices to the class Service. Of course, you can use any other string that uniquely identifies the XML elements. You can use the URL link to your company's page. It is not necessary that the Web link really exist; it is just used for unique identification. If you use a namespace based on your company's Web address, you can almost guarantee that no other company is using the very same namespace.

    If you don't change the namespace, the default namespace used is http://tempuri.org. For learning purposes, this default namespace is good enough, but you shouldn't deploy a production Web service using it.

  2. So, modify the example code as follows:

     [WebService(Namespace="http://www.wrox.com/webservices")] [WebServiceBinding(ConformsTo=WsiProfile.BasicProfile1_1)] public class Service : System.Web.Services.WebService {
  3. Now compile the project.

image from book




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

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