Building Microsoft ASP.NET Applications for Mobile Devices, Second Edition [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Building Microsoft ASP.NET Applications for Mobile Devices, Second Edition [Electronic resources] - نسخه متنی

Andy Wigley; Peter Roxburgh

نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
افزودن یادداشت
افزودن یادداشت جدید






Creating an XML Web Service

This first example will show you just how easy creating an XML Web service is. In keeping with tradition, you'll write a "Hello World" XML Web service. You'll do this first with the powerful command-line tools the .NET Framework provides and then by using Visual Studio .NET, which provides helpful wizards to ease the development process.


Creating an XML Web Service Using a Text Editor


You store XML Web services in files that have an .asmx extension in a virtual directory on your Web server, just like standard mobile Web pages. Create a virtual directory, and name it MyFirstWebService. Using a text editor, create a new file named MyWebService.asmx. Listing 15-1 shows the code you should write and save in the file.

Listing 15-1: Source file MyWebService.asmx






<%@ WebService Language="c#" %>
using System;
using System.Web.Services;
[WebService(Namespace="http://127.0.0.1/MyFirstWebService/")]
class MyWebService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}











Although this code resembles a typical ASP.NET "Hello World" program, it has a few important differences:



The first line declares that the code is an XML Web service. You must include this declaration in all XML Web services.



You import System.Web.Services, which contains the WebService class your class will extend.



The [WebService…] attribute declares that the class that follows it describes a Web service. This attribute is optional and doesn't affect the operation of the class as a Web service. However, it does allow you to change the namespace, the name, and the description of the Web service. The XML namespace declaration should declare the unique namespace for the Web service to distinguish it from other Web services. The namespace usually takes the form of a URL but can be any name that is unique.



Your XML Web service class (MyWebService) extends the System.Web.Services.WebService class. You can inherit some useful methods and properties of this class; however, you don't have to inherit from this class to create an XML Web service.



The [WebMethod] attribute that precedes the HelloWorld method signifies that the HelloWorld method is accessible as an XML Web service. To make the method available as an XML Web service, you must declare it public.



That's it! You've completed your first XML Web service. Now you can either learn how to create this application in Visual Studio .NET or skip the next section to learn how to deploy and consume this XML Web service.


Creating an XML Web Service Using Visual Studio .NET


You can use Visual Studio .NET to easily create XML Web services. To create a new XML Web service, follow these steps:



Open Visual Studio .NET.



Click the File menu, then New… Project.



In the New Project dialog box, select your preferred language in the Project Types pane and then select the ASP.NET Web Service template from the Templates pane.



Give the project a name, such as MyWebService, and click OK. The main window will then change to display a Design view, as Figure 15-1 shows.


Figure 15-1: Visual Studio .NET Design view



Click the Click Here To Switch To Code View link. The window will update to show the XML Web service code-behind module.



Depending on your version of Visual Studio .NET, you might have a presupplied HelloWorld method. If so, you can just uncomment this method. If no HelloWorld method exists, add the following method to the code:



[WebMethod]
public string HelloWorld()
{
return "Hello World";
}

That's it! You've now created your first XML Web service using Visual Studio .NET. The source files you create in Visual Studio .NET are very similar to the solution we created in Listing 15-1 with a text editor. The primary difference is that Visual Studio .NET structures the XML Web service source files as an .asmx file (containing only an @ WebService directive), such as Service1.asmx, and a code-behind module (Service1.asmx.cs).

Your XML Web service is now ready to use, running on the IIS Web server on your development computer. You deploy XML Web services that you build using Visual Studio .NET to production Web servers in exactly the same way as an ASP.NET application, either by using the Copy Project facility or by building a Visual Studio .NET setup and deployment project. (This technique will be described in Chapter 17.)

/ 145