Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

C# ПІДРУЧНИКИ / c# / Hungry Minds - ASP.NET Bible VB.NET & C#

.pdf
Скачиваний:
128
Добавлен:
12.02.2016
Размер:
7.64 Mб
Скачать

Cache.Insert("ProductCatalog", Source, New

CacheDependency(Server.MapPath("product.xml")))

'Message to indicate that we created the cache lblMsg.Text = "Dataset created explicitly"

Else

lblMsg.Text = "Dataset retrieved from cache" End If

MyDataGrid.DataSource = Source

MyDataGrid.DataBind()

End Sub

</Script>

<Body>

<Form runat="server">

<H3><Font Face="Verdana">File Dependencies</Font></H3>

<ASP:DataGrid id="MyDataGrid" runat="server" Width="650" BackColor="#cccfff" BorderColor="black" ShowFooter="false" CellPadding=3 CellSpacing="0" Font-Name="Verdana"

Font-Size="8pt" HeaderStyle-BackColor="lightgreen" />

<Hr>

<P>

<I><asp:label id="lblMsg" runat="server"/></I></P> </Form>

</Body>

</Html>

The code of the Product.xml file used in the preceding Web page is given as follows: <Catalog>

<Product>

<id>1</id>

<name> Oranges </name> <qty> 120 </qty> <price>$2.95 </price>

</Product>

<Product>

<id>2</id>

<name> Apples </name>

<qty> 100 </qty>

<price>$2.65 </price>

</Product>

</Catalog>

The output of the preceding code is shown in Figure 17-9.

Figure 17-9: Output of the CacheDependency Web page

Note If the content of the Product.xml file is updated manually or otherwise, the Web page will automatically refresh the cache, and the message "Dataset created explicitly" will be displayed.

Key-based dependency

Key-based dependency invalidates a particular cache item when another cache item changes. For example, consider an application that adds multiple datasets to the cache, such as ProductData, SalesData, and MarketingData. If the SalesData and MarketingData datasets rely upon the ProductData dataset for data validation, you can use a key-based dependency to invalidate SalesData and MarketingData if the ProductData item changes. To do so, you need to set up this dependency when you create the cache entries for SalesData and MarketingData:

Dim dependencyKey(1) As String

dependencyKey(0) = "ProductData"

Dim productDataDependency As new CacheDependency

(nothing, dependencyKey)

Cache.Insert("SalesData", LoadDataSet("Sales"),

productDataDependency)

In this code, productDataDependency is the object of the CacheDependency class. The dependency key, which is set to ProductData, is passed as a parameter to instantiate the object. Now, whenever ProductData changes, SalesData will be removed from the cache. Similarly, you can extend the code to incorporate a dependency on the MarketingData dataset.

Time-based dependency

Time-based dependency causes an item to expire at a defined time. Again, the Insert() method of the Cache class is used to create a time-based dependency. Two options are available for the time-based dependency:

§Absolute: Sets an absolute time for a cache item to expire. This option is best suited for data that changes on a periodic basis and at a known time.

§Sliding: Resets the time for the item in the Cache to expire on each request. Therefore, an item remains in the cache for the specified time; if no requests are made for that item, it automatically expires from the cache. If requests for that item are received, the cache duration is automatically extended. Therefore, this option is useful when an item in the cache is to be kept alive so long as requests for that item are coming in from various clients.

For example, to cache the ProductData dataset for a maximum duration of 10 minutes, you can use the Sliding option:

'set a 10 minute time span

Dim span As New TimeSpan(0,10,0)

'Add the return data from the LoadDataSet method into

the 'Cache. The item will be identified as ProductData and will

'be stored in the cache for 10 minutes

Cache.Insert("ProductData", LoadDataSet(), nothing,

nothing, span)

In addition to the dependencies, ASP.NET allows the following:

§Automatic expiration: The cache items that are underused and have no dependencies are automatically expired.

§Support for callback: The Cache object can be configured to call a given piece of code that will be executed when an item is removed from the cache. This gives you an opportunity to update the cache.

For example, you can guarantee that the item (ProductData dataset) is always served from the cache. To do so, add the following code in the GetData method:

'Declare a callback method for notifying applications when

'a cached item is removed from the cache

Dim onRemoveItem As New CacheItemRemovedCallback(AddressOf

Me.RemovedCallback)

'Cache the item. Notice the last parameter, which is the

'callback method. Also, notice that the priority of the

'item has been kept high and the decay has been set to

'slow

Cache.Insert("ProductData",ds,nothing,DateTime.Now.

AddSeconds(5),TimeSpan.Zero,CacheItemPriority.High,

CacheItemPriorityDecay.Slow,onRemove)

'Removing the item to invoke the callback method

Cache.Remove("ProductData")

Then, create the callback method that should be invoked when the cache item is removed.

'Define the callback method

Public Sub RemovedCallback(k as String, v as Object,

r As CacheItemRemovedReason)

'loading the item from cache

GetData()

End Sub

Because the Cache object is maintained by ASP.NET depending on the usage of items, it is likely that items in the cache are removed if they are not used or are underused. Hence, you must ensure that the applications that use the Cache object always check for the presence of an item in the cache before attempting to retrieve the item.

Summary

This chapter introduced you to caching in ASP.NET. First, you learned about the Cache API provided by the .NET Framework. Then, you saw the different Cache Performance Monitor counters. Finally, you learned how to use the Cache API for page output and page data caching.

Chapter 18: Building Wireless Applications with ASP.NET Mobile Controls

Overview

The Internet is constantly evolving, and has moved from the desktop to include the wireless world. As an extension of ASP.NET, Microsoft has released the Mobile Internet Toolkit (MIT). MIT is an intelligent solution to produce mobile applications that detect the browsing device and return the appropriately formatted content. Thus, MIT provides a single application that adapts to Web-enabled cell phones, pagers, and personal digital assistants (PDAs).

This chapter provides an overview of the Wireless Application Protocol (WAP) and the basics of the Wireless Markup Language (WML). After you understand how your data is transferred to your wireless devices, you will learn about the MIT controls that produce a single application that is then available to multiple devices.

Introduction to Mobile Development

There are many challenges and obstacles we need to consider when developing mobile applications. You will start by learning what some of these challenges are, and how you can deal with them using the MIT.

Challenges to Mobile Development

Many people have become reliant on the information that is available on the Internet. However, whereas people traditionally have used their desktop computer to access the Internet, they increasingly are relying upon mobile devices to access the Internet. Although the technology exists to extend your desktop applications to a mobile environment, you have to be aware of some of the limitations of mobile devices:

§Smaller screen size: A typical cell phone can only display 15 to 20 characters across and between 4 to 6 lines of text.

§Power: Most mobile devices have limited battery life, memory, and processing power, and do not carry the same capabilities as your desktop PC.

§Bandwidth: By nature, wireless applications are more costly to run and, technically, cannot provide the bandwidth found on a wired network.

Luckily, MIT will handle most of the screen size limitations that you will run into when developing wireless applications. MIT will dynamically detect the device being used and provide the appropriate output. However, you still must remember that screen real estate will be at a premium when developing your applications. Along with real estate, bandwidth is also at a premium. You must be able to compress your applications into small chunks of data. Obviously, you won't be sending streaming video down to your cell phone (at least not yet), but you may be sending images to enhance the user experience.

In spite of the difficulties previously mentioned, MIT makes it easy to deploy wireless applications, and it does so intelligently.

Wireless devices and emulators

Over 80 percent of Internet-enabled, wireless devices consist of cell phones and PDAs. Cell phones have a typical display screen of 15 to 20 characters and between 4 to 6 lines of text. PDAs are a little bit larger, and may be as wide as 20 to 25 characters, and include up to 6 to 10 lines of text. Testing your applications on wireless devices could get to be expensive over the process of building your software. Therefore, to test your applications, this chapter explains how to use two different emulators: a cell phone emulator by Openwave, and the Pocket PC emulator available from Microsoft. Openwave is one of the world's largest providers of mobile Internet software. You can download its cell phone emulator (Openwave SDK) from http://developer.openwave.com . After you've installed the SDK on your system, it may look similar to Figure 18-1.

Figure 18-1: Using the Openwave cell phone emulator

Microsoft provides an emulator for its Windows CE device. This emulator is part of the Microsoft eMbedded Visual Tools kit. Like the Openwave SDK, both the Openwave SDK and the PocketPC emulator are standalone Windows applications. Figure 18-2 shows the opening screen of the Pocket PC emulator.

Figure 18-2: Using the Pocket PC emulator

WAP, WML, and a deck of cards

The Wireless Application Protocol (WAP) is used to transfer your data to your wireless devices. WAP was conceived by four companies: Ericsson, Motorola, Nokia, and Unwired Planet (now Openwave). These four companies founded the WAP Forum (http://www.wapforum.com), which has now grown to over 200 members, including operators, infrastructure suppliers, software developers, and content providers.

Because WAP is the protocol used to transfer our data, we need another protocol to format our data. That's where WML, or Wireless Markup Language, comes into play. WML is used to format pages that are delivered using WAP. WML has its roots in XML, and is in fact still XML-compliant.

Note Because WML is XML-compliant, it is case-sensitive and must have a closing tag for every opening tag.

Each WML file is made up of a deck of cards. Just as a traditional "deck of cards" contains individual playing pieces, a WML deck of cards contains individual screens. Although a complete deck is normally sent to your mobile device at a time, only one card can be seen at a time. A card can be thought of as a single page or screen. Thus, by sending a deck of cards to your mobile device, you are actually sending multiple pages at a time. As an example, Figure 18-3 displays the layout of a deck of cards that we will use to select a bicycle.

Figure 18-3: WML deck of cards

Building Your First Mobile Application

Now that you understand the foundation of mobile applications, you will build your first application. Your sample application, about bicycling, will demonstrate various techniques of displaying data, and receiving input data from the user.

Static pages

Let's take our layout and convert it to an application. However, before we start coding, we need to make sure our Internet Information Server (IIS) is set up to properly serve WML pages. To configure IIS 5.0 on Windows 2000 to serve up static WML pages, follow these steps:

1.Open Control Panel, then Administrative Tools, then IIS administrator.

2.Right -click your Web site instance and select Properties.

3.Click the HTTP Headers tab.

4.Under Mime Types, click File Types.

5.Under Registered File Types, check to see if .wml is listed.

6.If .wml is not listed, click New Type to show the File Type dialog box.

7.Enter ".wml" (without quotes) in the Associated Extension box.

8.Enter "text/vnd.wap.wml" (without quotes) in the Content Type (MIME) box. Figure 18-4 shows the File Type dialog box.

Figure 18-4: Registering .wml as a MIME type

The following code provides an example of a deck of cards. This WML deck contains 3 cards that provide links between each card.

<? xml version="1.0" ?>

<! DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1 //EN" "http://www.wapforum.org/DTD/wml_1.1.xml">

<wml>

<card id="firstcard" title="Bike Style"> <p>

Select a Bike Style <br />

<a href="#secondcard">Road Bike</a> <br />

<a href="#thirdcard">Mountain Bike</a> </p>

</card>

<card id="secondcard" title="Road Bike"> <p>

<b>You Selected a Road Bike!</b> </p>

</card>

<card id="thirdcard" title="Mountain Bike"> <p>

<b>You Selected a Mountain Bike!</b>

</p>

</card>

</wml>

Our deck contains three cards. Based upon which bike style you select, you will be taken to a different card. This example links within the same deck. When this WML file is downloaded to the device, the entire deck will be resident in memory. Figure 18-5 shows our newly created WML file.

Figure 18-5: Bike.wml

 

Note

All WML files must begin with:

<? xml version="1.0" ?>

<! DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1 //EN"

"http://www.wapforum.org/DTD/wml_1.1.xml">

Note Because <br> tags normally don't have a closing tag, it is considered an empty element, and closed with a trailing slash, like

<br />.

Our example currently links cards together. Sometimes we need to link decks together. The following code demonstrates linking decks together:

[bike.wml]

<? xml version="1.0" ?>

<! DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1 //EN"

"http://www.wapforum.org/DTD/wml_1.1.xml">

<wml>

<card id="firstcard" title="Bike Style">

<p>

Select a Bike Style From a Deck

<br />

<a href="roadbike.wml">Road Bike</a>

<br />

<a href="mountainbike.wml">Mountain Bike</a>

</p>

</card>

</wml>

Installing the Mobile Information Toolkit

Currently, MIT is available as an additional download from http://www.asp.net. The installation process is straightforward and painless. After you have downloaded the EXE file, double-click the file to start the Microsoft Installer (or MSI) InstallShield Wizard.

Figure 18-6 displays the start page of the InstallShield Wizard for MIT.

Figure 18-6: Installing MIT

Clicking the Next button provides you with several options, including Mobile QuickStart, Mobile Internet Designer for Visual Studio .NET, and Documentation, as shown in Figure 18-7. If you have the space available on your system, you should choose to install all the files.

Соседние файлы в папке c#