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

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

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

460 Chapter 10 • Web Services

<port name="getCategoriesHttpGet" binding="s0:getCategoriesHttpGet">

<http:address

location="http://ubid/bookSource/getCategories.asmx" />

</port>

<port name="getCategoriesHttpPost" binding="s0:getCategoriesHttpPost">

<http:address

location="http://ubid/bookSource/getCategories.asmx" />

</port>

</service>

</definitions>

Developing & Deploying…

When Moving a VS.NET Web

Service to Another Server

When transferring a project to another server, make sure the page namespaces match the project name and be sure to update Web references.

Using XML in Web Services

Web Services use SOAP as a messaging protocol. SOAP is a relatively simple XML language that describes the data to be transmitted.Why use XML? XML is a standard language designed to be understandable by humans, and structured so it can be interpreted programmatically. XML does not only describe data, it can also describe structure, as we will see when we take a closer look at the ADO.NET DataSet.

Consider the case of replicating a database into cache.We might want to do this to reduce the load on the database server, to speed client processing, or to provide an offline data handling scenario.We could transport an XML document that contains the new W3C XML Schema Definition Standard (XSD) schema describing the database tables, relations, and constraints, along with the actual data (see the section “Using DataSets” later in this chapter). Because XSD can

www.syngress.com

Web Services • Chapter 10

461

describe relational data and can be embedded within an XML document, any database can be converted to a ubiquitous data source.That is, a data source that can be accessed on any platform by any application.This is possible because the transfer protocol, SOAP, uses XML over HTTP and because XML, XSD, SOAP, and HTTP are all nonproprietary industry standards.

It is the use of non proprietary industry standards that makes Web Services so powerful. By using XML to describe structure and content,Web Services can provide an interface to data on legacy systems, or between incompatible platforms from acquisitions or between vendors over intranets, extranets, or the Internet.

An Overview of the

System.Web.Services Namespace

System.Web.Services is the namespace from which all Web service classes are derived. It consists of all the classes needed to create Web Services in the .NET Framework.When using VS.NET most of the System.Web.Services classes and subclasses are transparent to the developer, so we won’t go into much depth here. The three primary child classes of System.Web.Services are: Description, Discovery, and Protocols.

The System.Web.Services.Description

Namespace

The System.Web.Services.Description namespace contains the classes needed to describe a Web Service using the Microsoft SDL (Service Definition Language), a Microsoft implementation of the WSDL standard.VS.NET uses these classes to create the .disco or .vsdisco file. Many of the subclasses of this class are related to binding: MessageBinding, OperationBinding, OutputBinding, and so on. One of the more interesting subclasses is the ServiceDescription class. It takes as a parameter an XML file and enables the creation of a valid WSDL file.

ServiceDescription MyDescription = new ServiceDescription();

ServiceDescription MyDescription =

ServiceDescription.Read("MyTestFile.xml");

The System.Web.Services.Discovery Namespace

The System.Web.Services.Discovery namespace consists of the classes that enable Web Service consumers to locate available Web Services. In VS.NET when we

www.syngress.com

462 Chapter 10 • Web Services

create a Web Reference, these classes find the .vsdisco files that describe Web Services.

Disco file from our Hello World example:

<?xml version="1.0" encoding="utf-8"?>

<discovery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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

xmlns="http://schemas.xmlsoap.org/disco/">

<contractRef

ref="http://localhost/WebApplication_HelloWorld/hello.asmx?wsdl"

docRef="http://localhost/WebApplication_HelloWorld/hello.asmx"

xmlns="http://schemas.xmlsoap.org/disco/scl/" />

</discovery>

The System.Web.Services.Protocols Namespace

The System.Web.Services.Protocols namespace consists of the classes used to define the protocols that enable message transmission over HTTP between ASP.NET Web Services and ASP.NET Web Service clients.These classes are used in our WSDL proxy classes.They are mostly involved with the formatting, bindings, and settings of the SOAP message.

WSDL proxy from our Hello World example:

namespace WebApplication_HelloWorld.localhost { using System.Diagnostics;

using System.Xml.Serialization; using System;

using System.Web.Services.Protocols; using System.Web.Services;

[System.Web.Services.WebServiceBindingAttribute(Name="helloSoap",

Namespace="http://tempuri.org/")]

public class hello : System.Web.Services.Protocols.SoapHttpClientProtocol {

[System.Diagnostics.DebuggerStepThroughAttribute()] public hello() {

www.syngress.com

Web Services • Chapter 10

463

this.Url =

"http://localhost/WebApplication_HelloWorld/hello.asmx";

}

[System.Diagnostics.DebuggerStepThroughAttribute()] [System.Web.Services.Protocols.SoapDocumentMethodAttribute(

"http://tempuri.org/HelloWorld",

Use=System.Web.Services.Description.SoapBindingUse.Literal,

ParameterStyle=

System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]

public string HelloWorld() {

object[] results = this.Invoke("HelloWorld", new

object[0]);

return ((string)(results[0]));

}

[System.Diagnostics.DebuggerStepThroughAttribute()] public System.IAsyncResult BeginHelloWorld(

System.AsyncCallback callback, object asyncState)

{

return this.BeginInvoke(

"HelloWorld", new object[0], callback, asyncState);

}

[System.Diagnostics.DebuggerStepThroughAttribute()]

public string EndHelloWorld(System.IAsyncResult asyncResult) { object[] results = this.EndInvoke(asyncResult);

return ((string)(results[0]));

}

}

}

www.syngress.com

464 Chapter 10 • Web Services

Type Marshalling

Type marshalling refers to the translation of datatypes from an application or database as it is mapped to a SOAP datatype.When any datatype, object, method, or string (xml, or a simple string) is passed as a SOAP request or response, it is automatically converted into an XML representation of itself. Since any programming language can use SOAP, SOAP has defined its own set of datatypes.When data is passed in a SOAP envelope its datatypes are translated or converted to a SOAP equivalent.This enables different languages with different names for similar datatypes to communicate effectively.The datatypes supported when using Web Services include:

Standard primitive types String, char, Boolean, byte, single, double, DateTime, int16, int32, int 64, Uint16, and so on.

string "hello World" is represented as:

<string>hello World</string>

Enum Types Enumerations like enum weekday {sun=0, mon=1, tue=2, wed=3, thu=4, fri=5, sat =6}

Arrays of Primitives or Enums

MyArray[ 5,7 ] is represented as:

<ArrayOfInt>

<int>5</int>

<int>7</int>

</ArrayOfInt>

Classes and Structs

struct Order( OrderID, Price ) is represented as:

<Order>

<OrderID>12345</OrderID>

<Price>49.99</Price>

</Order>

Arrays of Classes (Structs)

MyArray Orders( order1, order2 ) may be represented as:

<ArrayOfOrder>

www.syngress.com

Web Services • Chapter 10

465

<Order>

<OrderID>int</OrderID>

<Price>double</Price>

</Order>

<Order>

<OrderID>int</OrderID>

<Price>double</Price>

</Order>

</ArrayOfOrder>

DataSets The representation of a DataSet is rather lengthy; it includes an inline XSD schema defining the structure followed by the XML data. For an example of a DataSet, see the next section,“Using DataSets.”

Arrays of DataSets

XmlNodes

<book id=1><title>book1</title><price>25.00</price></book>

Arrays of XmlNodes

<ArrayOfBook>

<book id="1">

<title>book1</title>

<price>25.00</price>

</book>

<book id="2">

<title>book2</title>

<price>49.99</price>

</book>

</ArrayOfBook>

It is important to note that when we create and use Web Services in VS.NET, the marshalling of data is transparent to the developer.This is also true when using the WSDL.exe command line utility.While it is important to have some understanding of how data is transported between the Web Service and the Service proxy or client, this layer is and should be transparent to the developer, just as packet structures for transmitting data over HTTP is transparent to the Web developer.

www.syngress.com

466 Chapter 10 • Web Services

Using DataSets

A DataSet can be used to cache an entire database within an ASP application variable.This would reduce the Database Server Load and speed data access over the life of the application object.The following is a code snippet that calls a Web Service that returns a DataSet.The DataSet in turn stores the data in an application object.

myServer.getBooks DataSource = new myServer.getBooks();

Application["AllBooks"] = DataSource.AllBooks();

This makes the DataSet available to all instances of the Web application, which is very efficient. Operations can be performed on the DataSet and, on Application_End the Database can be updated.

DataSets store database structure information and contain DataTable,

DataColumn, DataRow, and DataView children. DataSet RowFilter operations are very much like SQL Queries.The DataSet can easily be databinded to ASP.NET UI controls. It also has an XML output format that makes it easily translated to XML for XML processing.

Here is an example of the Books DataSet returned by the getBooks.allBook service:

<?xml version="1.0" encoding="utf-8"?> <DataSet xmlns="http://tempuri.org/">

<xsd:schema id="NewDataSet" targetNamespace="" xmlns="" xmlns:xsd=http://www.w3.org/2001/XMLSchema xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xsd:element name="NewDataSet" msdata:IsDataSet="true">

<xsd:complexType>

<xsd:choice maxOccurs="unbounded"> <xsd:element name="Books">

<xsd:complexType>

<xsd:sequence>

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

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

<xsd:element name="id" type="xsd:int" minOccurs="0" />

www.syngress.com

 

 

 

Web Services • Chapter 10

467

<xsd:element

name="imgSrc"

type="xsd:string"

 

minOccurs="0"

/>

 

 

<xsd:element

name="author"

type="xsd:string"

 

minOccurs="0"

/>

 

 

<xsd:element

name="price" type="xsd:decimal"

 

minOccurs="0"

/>

 

 

<xsd:element

name="title" type="xsd:string"

 

minOccurs="0"

/>

 

 

<xsd:element

name="description" type="xsd:string"

 

minOccurs="0"

/>

 

 

</xsd:sequence>

 

 

 

</xsd:complexType>

 

 

 

</xsd:element>

 

 

 

 

</xsd:choice>

 

 

 

 

</xsd:complexType>

 

 

 

 

</xsd:element>

 

 

 

 

</xsd:schema>

 

 

 

 

<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"

 

xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">

 

<NewDataSet xmlns="">

 

 

 

 

<Books diffgr:id="Books1"

msdata:rowOrder="0">

 

<isbn>0072121599

</isbn>

 

 

<name>cisco</name>

 

 

 

 

<id>2</id>

 

 

 

 

<imgSrc>ccda.gif</imgSrc>

 

 

<author>Syngress Media

Inc</author>

 

 

<price>49.99</price>

 

 

 

 

<title>Ccda Cisco Certified Design Associate Study Guide</title>

 

<description>

 

 

 

 

Written for professionals intending on taking the CCDA test,

 

this special guide covers all the basics of the test and

 

includes hundreds of test questions on the enclosed CD.

 

</description>

 

 

 

 

</Books>

 

 

 

 

www.syngress.com

468 Chapter 10 • Web Services

<Books diffgr:id="Books2" msdata:rowOrder="1"> <isbn>0072126671 </isbn> <name>cisco</name>

<id>2</id>

<imgSrc>ccna.gif</imgSrc>

<author>Cisco Certified Internetwork Expert Prog</author> <price>49.99</price>

<title>CCNA Cisco Certified Network Associate Study Guide</title>

<description>

Cisco certification courses are among the fastest-growing courses in the training industry, and our guides are

designed

to help readers thoroughly prepare for the exams. </description>

</Books>

</NewDataSet>

</diffgr:diffgram>

</DataSet>

www.syngress.com

Web Services • Chapter 10

469

Summary

In this chapter, we discussed Web Services, along with their related technologies, protocols, and standards, such as Simple Object Access Protocol (SOAP),Web Services Description Language (WSDL), Extensible Markup Language (XML), and the XML Schema Definition (XSD) standard.We examined the role of Web Services and how messages are passed between servers and data sources.

We created simple Web Services (producers) as well as Web Services (consumers) using the .NET Framework and VS.NET Beta 2 to show how the Web Service messaging infrastructure works and how it can be used transparently to the developer.

The power of Web Services is due to its foundation in nonproprietary protocols and standards.Web Services would not be as useful if it were not built on XML for defining data and structure, XSD for defining structure, SOAP for defining a messaging transport mechanism over the well-established HTTP, WSDL for defining method interfaces in XML, Universal Description, Discovery, and Integration (UDDI, a Web Service discovery mechanism), and DISCO, the Web Service discovery description document.

We’ve covered a lot of ground here. For in-depth examples, see Chapter 12, where we develop several Web Services as wrappers around our data source, for use by an ADO application.

Solutions Fast Track

Web Services

;Web Services provide an XML interface that can be accessed by any SOAP-enabled client, which means a Web Service developed with .NET can be accessed by a Java application, a Web page, or any SOAP-enabled desktop application.

;Web Services can be accessed over HTTP through port 80, which means remote procedure calls can be made to objects behind firewalls.

Using XML in Web Services

;XML is the enabling standard upon which SOAP and Web Services are built.

www.syngress.com