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

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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






next section), various arrays, and the standard value types, such as Char and Int32. All these have to be encoded in XML for transfer between client and server. Value types translate directly to primitive types in the XML Schema Definition (XSD) type system, whereas the .NET XML Serializer class is used to serialize classes and complex data types. Table 15-3 shows a brief summary of the permitted data types. For a complete list of the data types XML Web services can return, refer to the .NET Framework SDK documentation.

































Table 15-3: Permitted XML Web Service Data Types


Type


Description


Arrays


Arrays of any of the types mentioned in this table.


Classes and structures


An XML Web service will return the public properties and fields of classes or structures.


DataSet


You can include DataSet objects as fields in classes or structs. We'll discuss DataSet objects in the next section.


Enumerated types


The values of enumerations.


Primitive types


You can use any of the standard data types: String, Char, Byte, Boolean, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double and Decimal.


Reference Types


You can use certain .NET Framework reference types: Guid (globally unique identifier), DateTime (as XML's timeInstant, date, or time), and XmlQualifiedName (as XML's QName).


XMLNode


An XML node is an XML fragment held in memory. (See the following note for more details.)






Note

You can pass fragments of XML to a Web method. You store these fragments within an XMLNode object. For example, you can store <myxml>Here it is!</myxml> in an XMLNode. You can pass an XMLNode as a parameter to an XML Web service, and an XML Web service can return an XMLNode.


Although simple, the code in Listing 15-3 illustrates how you pass data types other than strings to an XML Web service and how that service returns different data types. More specifically, an array of floating-point values is passed to the XML Web service, with the values representing two sides of a right triangle. The GetHypotenuse method calculates the length of the hypotenuse and returns a floating-point value that equates to the length of the hypotenuse. In addition, the XML Web service offers a GetTriangleStats method, which accepts an array of lengths as a parameter and returns a user-defined Statistics object.

Listing 15-3: Code-behind file Service.asmx.cs of DataTypesWebService example that uses array parameters and returns a class






using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace MSPress.MobWeb.DataTypesWebService
{
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public double GetHypotenuse(double[] sides)
{
return Math.Sqrt(Math.Pow(sides[0],2)+ Math.Pow(sides[1],2));
}
[WebMethod]
public Statistics GetTriangleStats(double[] sides)
{
// Create a new Statistics object.
Statistics myStats=new Statistics();
// Calculate the area.
double s1=Math.Min(sides[0],sides[1]);
double s2=Math.Min(Math.Max(sides[0],sides[1]),sides[2]);
myStats.area=(s1*s2)/2;
// Calculate the perimeter.
myStats.perimeter=sides[0]+sides[1]+sides[2];
// Return the properties of the class instance.
return myStats;
}
}
public class Statistics
{
// Declare fields.
public double area;
public double perimeter;
}
}











To consume the XML Web service, you might want to write a mobile Web Forms page that collects input from a user and then passes that input to the service. However, for the sake of brevity, this example will use hard-coded values in a mobile Web Forms page code-behind module. The mobile Web Forms page (whose code isn't shown here but that you can find in the ConsumeDataTypesWebService sample in the companion material on this book's Web site) simply consists of three Label controls. Listing 15-4 shows the code-behind module for this page.

Listing 15-4: MobileWebForm1.aspx.cs of the ConsumeDataTypesWebService sample






using System;
using System.Web;
using System.Web.Mobile;
using System.Web.UI;
using System.Web.UI.MobileControls;
using ConsumeDataTypesWebService.localhost;
namespace MSPress.MobWeb.ConsumeDataTypesWebService
{
public class MobileWebForm1 : System.Web.UI.MobileControls.MobilePage
{
protected System.Web.UI.MobileControls.Form Form1;
protected System.Web.UI.MobileControls.Label Label2;
protected System.Web.UI.MobileControls.Label Label3;
protected System.Web.UI.MobileControls.Label Label1;
public MobileWebForm1()
{
Page.Init += new System.EventHandler(Page_Init);
}
private void Page_Load(object sender, System.EventArgs e)
{
// Create a new instance of the XML Web service.
Service1 service1=new Service1();
// Create an array of values.
double[] sides=new double[3];
sides[0]=10;
sides[1]=10;
// Pass to the XML Web service.
sides[2]=s.GetHypotenuse(sides);
// Get statistics.
Statistics myStats = service1.GetTriangleStats(sides);
// Set three labels to show the return values.
Label1.Text="Hypotenuse length: " + sides[2].ToString();
Label2.Text="Area: " + myStats.area.ToString();
Label3.Text="Perimeter:" + myStats.perimeter.ToString();
}
private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
}











Be aware that in Listing 15-4, an instance of the Statistics class (the instance created in the XML Web service) doesn't actually return to the client. Instead, the properties and fields of that instance return to the client. The runtime creates a new object on the client and populates that object with the properties the XML Web service returns. Although this new object might look like an instance of the original Statistics object, it isn't. You can only access the properties and fields of the original object, not its methods.

Figure 15-7 shows the output this code yields.


Figure 15-7: The three Label controls display values from the Statistics object fetched from the Web service.

/ 145