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

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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






Defining XML Web Service Behavior

When you created your first XML Web service, you placed a WebMethod attribute before the method you wanted to make publicly accessible as a Web method. This directive accepts a number of optional arguments that allow you to define certain behaviors and characteristics of the XML Web service method. Table 15-1 outlines the most common arguments of this attribute.



























Table 15-1: Common Properties of the WebMethod Attribute


Property


Type


Description


BufferResponse


Bool


When the value is True, the IIS runtime buffers the output from the XML Web service in memory until the response is complete. The runtime then sends this buffered response to the client. If the value is False, the IIS runtime still buffers the output, but the size of the buffer is limited to 16KB. The default value is True.


CacheDuration


int


The length of time for which a server caches the response. This can be useful if the Web service is likely to handle several requests for the same information in a short period of time. The caching engine caches responses according to the parameters used to call the Web method. You probably won't need to enable caching; the default value of 0 seconds disables caching.


Description


String


Describes the Web method. This description is displayed in the service description for the XML Web service.


EnableSession


Bool


Indicates whether session state is enabled or disabled. The default value is False, which means that the session state is disabled. You'll learn more about using session state with XML Web services in the next section.


MessageName


String


The name or alias for the Web method. You can use this attribute to provide a publicly callable alias for a method as an alternative to its actual name. You might find this useful when a class contains more than one publicly accessible method with the same name (method overloads).


You'll likely use the Description and EnableSession attributes more than any of the others. You'll learn about session management in the next section. The following code fragment shows how to use the Description attribute:

[WebMethod (Description="Method returns a Hello World String" )]
public String HelloWorld() {
return "Hello World";
}

When you created your first XML Web service using command-line tools, you used a WebService attribute. This type of attribute is similar to a WebMethod attribute, except that it applies to the XML Web service as a whole rather than a single method. Table 15-2 shows the properties of this attribute that you'll use most frequently.





















Table 15-2: Common Properties of the WebService Attribute


Property


Type


Description


Description


String


Describes the XML Web service. This description is displayed in the service description for the XML Web service.


Name


String


Defines the name of the XML Web service. Its default value is the name of the class implementing the service. You use this name within the service description, and the service's Help page displays the name.


Namespace


String


Defines the XML namespace to which the XML Web service belongs. You must provide a namespace for all XML Web services that you write. The XML namespace uniquely identifies the Web methods it contains. When you create a Web service with Visual Studio .NET, the Web service gets the default namespace of http://tempuri.org. You should change this to your own unique namespace before putting your Web service into production.


You used the Namespace argument of the WebService attribute when you wrote your first application in this chapter. The other two arguments allow you to provide information that might be helpful to potential users of your XML Web service. You should always give these argument values. Doing so enhances the usability of the XML Web services you write.

Listing 15-2 shows the "Hello World" XML Web service you wrote earlier in the chapter—only now this service uses the WebService and WebMethod attributes to enhance its usability.

Listing 15-2: "Hello World" XML Web service using attributes






<%@ WebService Language="c#" %>
using System;
using System.Web.Services;
namespace WebService1
{
[WebService(Namespace="http://127.0.0.1/MyFirstWebService/",
Name="Quotes",
Description="Provider of quite useless quotes and phrases." )]
class MyWebService : System.Web.Services.WebService
{
[WebMethod(EnableSession=true,
Description="Returns the venerable Hello World string")]
public string HelloWorld()
{
return "Hello World";
}
}
}











Figure 15-6 shows the output that appears on a desktop Web browser when you view this new version of "Hello World" Web service.


Figure 15-6: MyFirstWebServiceWithAttributes—our new version of "Hello World" with added attributes values

/ 145