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

ASP .NET Web Developer s Guide - Mesbah Ahmed, Chris Garrett

.pdf
Скачиваний:
37
Добавлен:
24.05.2014
Размер:
7.32 Mб
Скачать

470Chapter 10 • Web Services

;The SOAP envelope is an XML document.The SOAP message, meanwhile, describes the data being passed as an XML representation of the original datatype or object.

An Overview of the System.Web.Services Namespace

;System.Web.Services is .NET Framework’s namespace of classes that enable .NET Web Services.The three primary subclasses or subnamespaces are:

1.System.Web.Services.Description Classes that support WSDL, used to define the methods, parameters, and datatypes of Web Services.

2.System.Web.Services.Discovery Classes that support UDDI and the generation of WSDL proxies for Web Service clients.

3.System.Web.Services.Protocols Classes that support the generation and customization of Web service protocols, and can be used for things such as creating custom SOAP headers.

Type Marshalling

;Type marshalling is the mapping of types from Web Service method calls to SOAP datatypes.

;When remote calls are made using Web Services and the SOAP protocol; datatypes and objects that are passed are represented as XML descriptions of themselves. (Datatypes are marshalled as one of many SOAP standard datatypes.)

Using DataSets

;DataSets are ADO.NET objects that provide database type operations.

;DataSets enable the transfer of database structure and content to and from Web Services.

www.syngress.com

Web Services • Chapter 10

471

Frequently Asked Questions

The following Frequently Asked Questions, answered by the authors of this book, are designed to both measure your understanding of the concepts presented in this chapter and to assist you with real-life implementation of these concepts. To have your questions about this chapter answered by the author, browse to www.syngress.com/solutions and click on the “Ask the Author” form.

Q: Why replace COM objects with Web Services?

A:Web Services have a platform neutral interface.This enables Web Services to be easily utilized by multiple clients on different platforms developed with different programming languages. Note that existing COM components can be wrapped by Web Services.

Q: Can I create access to Web Services from a standard ASP page?

A: Yes, you can; however, you might want to look into Microsoft’s SOAP toolkit.

Q: How do I know I need Web Services?

A:If you have data that is needed by various customers (different departments, different levels of management, vendors, industry partners, consumers and so on) and getting to that data is hindered or prevented by issues involving platform, programming language, legacy hardware or other types of incompatibility, developing Web Services can help.

Q: What area of development are Web Services best for?

A:I believe that Web Services development like COM development will remain in the hands of the middle tier programmer.Traditionally this was accomplished with C++ and VB programmers, however simple data access type components may be developed by intermediate and advanced ASP developers.While this might still be the case, ASP.NET developers need a firmer grasp of programming with a strongly typed compiled language then their ASP predecessors.This makes the ASP.NET developer more of a middle tier programmer and less of a front-end Web developer. Since building and deploying Web classes and Web Services are relatively easy with VS.NET as compared to traditional COM development. I think the proportion of components built by the ASP developer (using ASP.NET) will be larger than it has been in the past.

www.syngress.com

472Chapter 10 • Web Services

Q:Is it possible to try out Web Services using only my local computer?

A:Yes, it is. Using the WSDL.exe command line tool, you can point to any Web server.This is even easier with the VS.NET UI. Simply right-click Web references, then select any Web service from the UDDI directory or your local machine, or simply type the URL of a disco file on any server.You can easily generate a WSDL proxy and use it as long as you are connected to the Internet.

Q:I’m currently in the process of migrating.What considerations should I take with my existing COM components?

A:Here are a few things to consider:

Who is the customer? If the customer is only within the intranet and there are no external customers in the foreseeable future, an existing DCOM infrastructure needn’t be changed.

What type of clients do I have? If the client is a desktop application, switching to Web Services would require updating the client, which may include updating the OS, and possibly the hardware so that the client has enough memory to host the .NET Framework.

Will I need to support Mobile devices in the near future? Using the .NET Mobile Framework to access Web Services is as simple as it is with

.NET. Updating the existing clients to .NET will make adding future clients simple and cost-effective.

www.syngress.com

Chapter 11

Creating an

XML.NET Guestbook

Solutions in this chapter:

Functional Design Requirements of the XML.NET Guestbook

Adding Records to the Guestbook

Viewing the Guestbook

Advanced Options for the Guestbook Interface

;Summary

;Solutions Fast Track

;Frequently Asked Questions

473

474 Chapter 11 • Creating an XML.NET Guestbook

Introduction

Your first case study is a simple online guestbook application, completely coded in ASP.NET.You are going to need to provide the basic functions through this guestbook, namely the ability to do the following:

1.Enable guests to enter messages.

2.Display all messages on one page.

3.Show author, e-mail address of author, and comment from the author of the message.

The flowchart in Figure 11.1 shows the user interaction process that you want to achieve.

Figure 11.1 Basic Functionality Layout

User

Add

View

Comment

Comment

 

In essence, the user will come to the site and decide if he or she wants to view previous messages or add new messages.The user will be redirected to the view comments page after filling out a new message, or the user viewing the messages has the option to fill out a message.

NOTE

In the CD there are two folders for this chapter, representing two ways of going about this guestbook: one is labeled “basic” and the other is labeled “advanced.” We are going to explore both of these.

www.syngress.com

Creating an XML.NET Guestbook • Chapter 11

475

All of these functions need to be kept as compact as possible. Our backend needs to store the following information for every message that is left on the guestbook:

Name

E-mail

Subject Line

Actual Comment

The Name, E-mail, Subject Line, and Actual Comment need to be required fields and you need to provide validation for the e-mail field. Also, you need to provide the user with an easy-to-use interface. A basic interface would consist of the user being able to do the following:

1.Choose between adding a new entry and viewing previous entries.

2.Properly locate the corresponding text areas for the entry points.

3.Have real-time validation take place where needed.

4.Reply to a comment left by a user via e-mail.

Functional Design Requirements

of the XML Guestbook

Several guestbooks are already available online for download, but most require either a Microsoft Access database or an SQL Server database for storing the guestbook entries and other information pertinent to that guestbook.While both of these tools provide their own strengths and weaknesses, you want to provide an application that is small, quick, and able to stand alone without requiring a separate application to make it work.This type of thought also implies that the application will be small and easy to transfer, if needed.You also need to keep an eye on the code and keep it as small as possible.You need to be able to write directly to the database and read from the database with as little code as possible. Just because you are trying to make the code portable doesn’t mean you need to make the code bloat!

So, if you are not going to use a traditional database (such as Microsoft Access or SQL Server), then what can you use that won’t kill the application requirements? Back in Chapter 8 we talked about a technology that is turning into a strong database alternative, called XML. XML will enable you to use a text-based

www.syngress.com

476 Chapter 11 • Creating an XML.NET Guestbook

approach to your database that does not rely on any ODBC connections or even any server (although your code will pretty much lock you into a server that uses ASP.NET). Also, through an XML schema you can define how our XML “row” will look and what each value must contain.

With your backend solution set at XML you need to determine how you are going to work with the XML file.The logical choice is the System.XML namespace, but you can actually find a faster method by using the XML tools that accompany the System.Data namespace. Even though System.XML is more powerful than System.Data when it comes to XML, you simply don’t need to rely on so much coding to see your results.

NOTE

The choice of System.Data over System.XML does not mean that System.XML is in any way inefficient. It simply means that, as programmers, we sometimes have to choose between a solution that requires more time but is more flexible, and a solution that is quicker but more rigid. System.XML is more flexible with XML than System.Data will ever be, but all you need for this case study is just to be able to read and write to an XML file. In other words, you are following an age-old adage of programming—K.I.S.—“Keep It Simple!”

Constructing the XML

Even though System.Data is viewed more or less as a method of working with traditional database connections, such as a SQL database or an Access database, it can also work with XML data, provided the XML has an inline schema that it can match the data against; almost like looking at the table structure first and then the data within it.

The file gbook.xml (shown in Figure 11.2, and in the Basic directory on the CD that accompanies this book) displays the XML code that we will be working with.

Figure 11.2 gbook.xml (Basic Version)

01: <gbook>

02: <xsd:schema id="gbook"

03: targetNamespace=""

Continued

www.syngress.com

Creating an XML.NET Guestbook • Chapter 11

477

Figure 11.2 Continued

04: xmlns=""

05: xmlns:xsd="http://www.w3.org/2001/XMLSchema"

06: xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

07:

08: <xsd:element name="gbook"

09: msdata:IsDataSet="true">

10:<xsd:complexType>

11:<xsd:choice maxOccurs="unbounded">

12:<xsd:element name="gbooky">

13:<xsd:complexType>

14:<xsd:sequence>

15:<xsd:element name="Name" type="xsd:string" minOccurs="0"

/>

16:<xsd:element name="Chrono" type="xsd:string" minOccurs="0" />

17:<xsd:element name="Email" type="xsd:string" minOccurs="0" />

18:<xsd:element name="Comments" type="xsd:string" minOccurs="0"

/>

19:</xsd:sequence>

20:</xsd:complexType>

21:</xsd:element>

22:</xsd:choice>

23:</xsd:complexType>

24:</xsd:element>

25:

26:</xsd:schema>

27:</gbook>

Lines 1 and 26 have the root tags for the XML file. In this example, we are using “gbook” but you can use anything. Lines 2 through 6 are one line that we used whitespace to organize the attributes in order for the tag to be more readable.The targetNamespace and xmlns attributes in the <xsd:schema> tag are left blank since both the targetNamespace and xmlns are inline.The xsd attribute is pointing to the current schema, and the special Microsoft attribute msdata points to a Microsoft data compatibility namespace.

www.syngress.com

478 Chapter 11 • Creating an XML.NET Guestbook

NOTE

If you want more information on the XSD and MSDATA attributes, you can find documentation for XML schemas online through http://msdn

.microsoft.com/library and www.w3.org/XML/Schema.html.

Lines 8 through 24 construct the element that will store the data.When the data is entered into the corresponding .aspx file, it will format the data within the XML per the data outline within the schema. In this case, a sample entry in our guestbook will appear as the following:

<gbooky>

<Name>Jon Ortiz</Name>

<Chrono>Time Posted</Chrono>

<Email>somewhere@overthereainbow.com</Email>

<Comments>Hola!</Comments>

</gbooky>

This information will be created by your application through the System.Data namespace. In order to be able to do it, System.Data matches the information input to the inline schema and creates the appropriate record. Now that you have set up the “template,” you can get started with the code that adds records. Refer to Figure 11.3 for the logic behind the XML file.

Adding Records to the Guestbook

Any veteran ASP developers are going to notice in this section a distinct change. Remember in desktop applications that you formed your GUIs using a Form? Well, in ASP.NET, the Form has been brought to Web development and is referred to as a Panel.You are going to work with your code inline for just this chapter so that you can get a good grasp of what a Panel looks like and how it works within ASP.NET.

There are no real differences between using a Form for desktop applications and a Panel for online applications. Many of the same subs are intact, such as OnLoad, and Panel can reference any item within the Panel, just like in desktops. A great place to view the Panel in ASP.NET is within the UI for adding guestbook records.Your file will be called add.aspx, and is the code is displayed in Figure 11.4 (note that some lines wrap), and in the Basic directory on the CD that accompanies this book.

www.syngress.com

Creating an XML.NET Guestbook • Chapter 11

479

Figure 11.3 Creating a Record Using the XML Schema

User enters information to submit a guestbook message.

Is the data valid?

NO

YES

ASP.NET guestbook entry page uses System.Data to read schema within XML file.

Does the Data pass validation and

match the XML NO Schema?

YES

New Row is created following the

Schema structure:

<gbooky>

<Name> User Name </Name> <Chrono> Time & Date Posted </Chrono> <Subject> Comment Subject </Subject> <Comment> Actual Message </Comment> <gbooky>

User is alerted to invalid data and asked to change data on the GUI.

www.syngress.com